RoGo Magazine - Beginner Lua Lesson 3: Services/API’s and Methods

.

Posted in Tutorials

October 9, 2014 at 12:22 AM

Beginner Lua Lesson 3: Services/API’s and Methods

By DerAtomik

Editors: Zaqre

Welcome back to Beginner Lua! Today, we’re going to discuss Services (A.K.A. API’s) and Methods.

Methods

A method is what’s called on an Instance (Basically any object in your game, from Parts to Scripts), or a Service. A method is used by putting a colon (:) on a particular Instance, followed by what method you want to call upon. There are a lot of different methods, almost every Instance and Service has their own unique ones. However, every ROBLOX Instance and Service shares the following methods: ClearAllChildren, Clone, Destroy, FindFirstChild, GetChildren, GetFullName, IsA, IsAncestorOf, & IsDescendantOf. Check the Wiki for more information of those methods.

With every method, there must be parenthesis after the method. Depending on the method, there may or may not be arguments. If there are arguments, the Wiki will provide the information of those arguments. Arguments are what goes inside the parenthesis. When you look at the arguments of a method on the Wiki, it will say what type of argument it is; String, IntValue, etc. Here is an example of checking if the Instance “Part” in the Workspace is a part:

--The IsA method is another way of checking the Class of an Instance--
--The only argument is string className--
if workspace.Part:IsA("Part") then
print("Part is a part!")
else print("Part is not a part.")
end

Services

A Service is called upon through a method on the Instance “game,” called “GetService.” Services are used to do many useful things, such as selling Catalog Items in-game, teleporting a player to a different place in-game, and more. Look on the Wiki for many useful Service API’s. Here’s an example of using the GamePassService API to check if a player owns a Game Pass:

local gps = game:GetService("GamePassService")

game.Players.PlayerAdded:connect(function(player)
--The arguments of the PlayerHasPass method are: Instance Player, numValue GamePassId--
if gps:PlayerHasPass(player, 1337) then
print(player.Name.." owns the Game Pass!")
end
end)

Click here to see the previous Beginner Lua Lesson

Click here to see the first Beginner Lua Lesson