Function

✏ Beginners Scripting Guide ✏

https://create.roblox.com/docs/luau/functions#basic-functionsarrow-up-right https://create.roblox.com/docs/scripting/events/remote#argument-limitationsarrow-up-right In this section, we will be discussing about function. A function is a code of block that can hold another code of block and can execute the code block that was stored in the function multiple times by using parentheses to call out the function.

MAKING A FUNCTION

Making a function is the same as making a variable. But it is just a little bit different. Instead of:

local myFunction

We can do it like this:

local function myFunction()

end

CALLING A FUNCTION

Now we have a function. But the function will not run yet unless you call it. To call it we will get it name and put parentheses after the name like this:

local function myFunction()
    print('Hello, World!!')
end

myFunction() -- Call the function to let it print "Hello, World!!" in the output

GLOBAL & LOCAL FUNCTION

A function also be GLOBAL and LOCAL. GLOBAL means it can be accessed everywhere in the script and LOCAL means it can be only accessed in a certain block of codes. It is the same as GLOBAL and LOCAL variable.

GLOBAL

LOCAL

ADDING PARAMETERS TO FUNCTION

Now let's say we want the function to print the total sum of 2 numbers that was given in the function when it was called. We can start doing that by making 2 parameters with different names like this:

Now we are adding 2 parameters together and printing it in the function:

Now we gonna call the function and pass in 2 arguments for the function:

USING RETURN IN FUNCTION

Return can be used to return data to the function or just break the function to prevent the code below from running.

RETURNING DATA BACK TO FUNCTION

To return data, we must make a function first and then use return and put the data that we want to return to the function after the return

RETURN A SUM TO FUNCTION

BREAKING A FUNCTION

Last updated