Fandom

ROBLOX Wikia

Functions for Beginners

12,816pages on
this wiki
Add New Page
Comments0 Share

This is a quick tutorial on using functions in Lua for beginners. This tutorial teaches you about the anatomy of function declarations in Lua and is meant to help guide you through the process of learning them using examples with explanations.

Tutorial 1: Basic Functions

Functions are used to repeat the same process multiple times in code. They are useful for doing custom iterations. Iterations are basically repeated performances, and in Lua; they are commonly used to go through (typically long) lists of items and doing certain things to them. The act of doing functions is called "calling" them (For example "I called function 1 first then function 2.")

Function declarations (making a function) is simple but requires thought. The first step is to actually declare the Lua keyword "function", then the function name, then parenthesis. After that, your code is put with any actions you may want to do in your code. To finish it off, just use the Lua keyword "end". Naming functions is exactly the same thing as naming variables, so only letters, numbers and the underscore (_) can be used in function names, as function names take up the variable name (e.g., If you have a function named "ticktock" you will overwrite the function if you assign the variable "ticktock" to something).

This first example is a ticker function. A ticker simply adds to a number when clicked, so this function will add to a variable and print a message "Tick!" when called.

x = 0 --define x as 0 

function myTicker() --name is "myTicker"
 x = x + 1 --add 1 to x
 print("Tick!")
end

It's pretty simple to do once you understand how to work it. This is simply defining the function, it has not been executed or called yet. To call a function, you simply state the function name with parenthesis after the name. When you call a function, it does not execute until the function has finished, then it returns to the line that you called it on.

print(x) --> 0
myTicker() --> Tick!
print(x) --> 1
myTicker() --> Tick!
print(x) --> 2
myTicker() --> Tick!
myTicker() --> Tick!
print(x) --> 4

This is basic usage of functions in normal Lua. Functions are useful for performing large actions at once, such as in minigame places and the minigame is more complex.

Tutorial 2: Functions with One Argument

No, functions don't have disagreements.

Sometimes you need to pass data along to a certain function without using a variable. Maybe you have an experience formula and you need to know what level you are based on the number of experience you have. Functions that have arguments will help you with this.

In the last examples, you can see that after the function name there is frequently two parenthesis with nothing in them. This shows that there are no arguments to the function. Arguments are pieces of data sent to a function to change how it works. Arguments have to be defined in functions in Lua by stating a list of variables in the function name's parenthesis when you declare it. Like so:

x = 0 --define x as 0

function myTicker(y) --name is "myTicker", with one argument "y"
 x = x + y --add y to x
 print("Tick!")
end

This function now uses one variable, "y" as an argument. The function now adds y to x when the function is called. If y is not given, or if it is not a number, the code will error. Normally functions will check for this if they are using user input. When you go to call a function with arguments, you simply put the variables or data right into the function's parenthesis. Here are some example calls. print(x) --> 0

myTicker(1) --> Tick!
print(x) --> 1
myTicker(2) --> Tick!
print(x) --> 3
myTicker(3) --> Tick!
myTicker(1) --> Tick!
print(x) --> 7

Tutorial 3: Functions with More Arguments

The last example only used one argument. What if you want to send more data to a function? You use more than one argument. It is possible for a function to use infinite arguments (Remember how print() takes infinite arguments?). Functions don't necessarily have to use any argument given to them. It would not cause errors. Here is the ticker function again, only using three arguments.

x = 0 --define x as 0

function myTicker(a, b, c) --name is "myTicker", with three arguments "a", "b", and "c"
 x = x + a + b + c --add a, b, and c to x
 print("Tick!")
end

Like any function that uses all variables passed to it, it will error if the function is called without all the data needed.

print(x) --> 0
myTicker(1, 0, 0) --> Tick!
print(x) --> 1
myTicker(1, 1, 0) --> Tick!
print(x) --> 3
myTicker(1, 1, 1) --> Tick!
myTicker(0, 0, 3) --> Tick!
print(x) --> 9

If you want to assert more accurate errors if data is missing from a function call, you can use the library function "assert" which will cause errors if the first argument is nil or false. Here's the function again, but with some security added to it to make it be more accurate with its errors.


function myTicker(a, b, c) --name is "myTicker", with three arguments "a", "b", and "c"
 assert(a, "a was not given") --does "a" exist?
 assert(b, "b was not given") --does "b" exist?
 assert(c, "c was not given") --does "c" exist?
 assert(type(a) == "number", "a is not a number") --is "a" a number?
 assert(type(b) == "number", "b is not a number") --is "b" a number?
 assert(type(c) == "number", "c is not a number") --is "c" a number?
 x = x + a + b + c --add a, b, and c to x
 print("Tick!")
end


Every single pre-made Lua function checks it's arguments before doing anything with them. Functions should always handle errors appropriately in Lua. This makes Lua simpler and easier to use, just like how it was meant to be made.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.