In this section, we’ll be learning about all basic types of functions you’ll need to know when scripting on Roblox. You could view functions as alot of things, one common way is as machines with defined tasks. And if we want to be a bit more accurate, functions are values that take up memory , receives thread control when called, and can be anonymous. Where the goals are portability, simplicity, small size, and scripting.
Define A Function: local function myFunction() -- myFunction is the name of the function. --Any code here, would run only when the function is called endNow, you might be asking 2 things: 1.When I run this code, it doesnt do anything, why? 2.What if I wanted to put values in the ( )?
Parameters & ArgumentsLet’s answer these questions and see how to implement it. 1.In order to make it actually do something/what the function is defined to do, you need to call the function. You can do this by declaring the function’s name.
local function myFunction() -- myFunction is the name of the function. print("This function is now running!") end myFunction() -- Calling the function to begin2.If we wanted to provide values into our function, that’s what parameters are for! Parameters allow us to edit and modify objects/values within our functions. You now might be wondering - how do I set parameters and use them? Let’s take a look here:
local function myFunction(String1,String2) -- myFunction is the name of the function. print(String1..String2) -- Hello!World end myFunction("Hello!","World") -- Calling the function to beginAs we can see, we he 2 parameters in out function - String1 and String2. And you might notice that when we’re calling the function,we provided 2 values,[ in this case, 2 string values] - those are called arguments. And it is important to match them accordingly. What I mean is: Hello! → String1 World → String2.
Returning dataLet’s assume we he the following function:
local function SumNumbers(n1,n2) local sum = n1+n2 print(sum) end SumNumbers(1,2) -- > 3 SumNumbers(2,5) -- > 7 SumNumbers(2,5-2) -- > 5 SumNumbers(2,3)-5 -- > errorWe can conclude a few conclusions: 1.We can call the function multiple times with different arguments. 2.We can’t perform any arithmetic actions with the result of the function yet.
And now, a question is asrising - how can we get the result [ the sum in this case], and perform arithmetic actions on it? The answer is return! Returning any data we want from the function, will help us use that data later on, again and again. Let’s return sum and see what happens:
local function SumNumbers(n1,n2) local sum = n1+n2 return sum end local CallingFunction1 = SumNumbers(1,2) print(CallingFunction1) -- 3 local CallingFunction2 = SumNumbers(1,2) + 5 print(CallingFunction2) -- 8 local CallingFunction3 = CallingFunction1 + CallingFunction2 print(CallingFunction3) -- 11You can notice that since we’re returning data and want to use it, we’re setting our function call as variables. This also allows us to easily do our stuff much faster. We could also do this if we wanted:
local function SumNumbers(n1,n2) local sum = n1+n2 return sum end local CallingFunction1 = SumNumbers(2,3) print(CallingFunction1 / 25) -- 0.2, since 5/25 is 1/5 which is 0.2 Anonymous FunctionsSo far, we’ve seen functions with names. But now, it’s time to learn a ‘new type’ of functions - the anonymous functions! They do not he names, and they’re usually connected to an event / callback. Let’s take a look:
local Players = game:GetService("Players") Players.PlayerAdded:Connect(function(Player) print(Player.Name.." has arrived!") end)I am sure all of you he seen this event and used alot of it. What it does is basically calls an event [PlayerAdded] which is an event for the Players service and connects it to a function. Within that function, we can provide a parameter, which refers to the player who joined in the game. This is useful in so many cases.
Variadic FunctionsVariadic Functions are functions that can accept an unlimited amount of arguments.[take for example the print() function:
print("Hello there John! did you know that :",1,"+",1,"=",2) print(string.format("This %s is a %s!", "post", "tutorial")) -- This post is a tutorial! Calling a Variadic Function with ArraysLet’s assume we had the following array local array = {2,4,8,10,12} We could do the following:
local array = {2,4,6,8,10,12} print( "The first 5 even numbers are:", unpack(array))Which basically ‘gets out’ all the items from the array , and arrange them in the string.