Roblox Wiki
Advertisement
Roblox Wiki

In Lua, a function is a block of code that can be called to perform a specific task and may be called multiple times. Functions can take arguments which are values provided by the call (simply known as inputs) and can return values to the call (simply known as outputs). Functions are represented by the "function" data type.

Syntax[]

A function is defined with a name, a list of parameters (local variables) and a body (a block of code). A return statement may be added at the end of the body or a scope within the body (with the exception of functions), optionally followed by return values, which ends the thread of the function call. Any more statements after the return statement will result in a syntax error.

function foo(a, b, c)
    print(a, b, c)
    return a + b + c
end

Functions may be called as a statement or as an expression. Function calls may be supplied with a list of arguments. Empty list () may be used to indicate a function call that has no arguments, otherwise it is interpreted as a variable.

foo(1, 3, 5)
v = foo(10, 30, 50)
print(time())

Functions may also return multiple values.

function foo(a, b, c, d)
    return a + b, c + d
end
v1, v2 = foo(5, 8, 11, 14)
print(v1, v2) -- 13, 25

Variadic functions[]

Variadic functions, such as the print function, take any number of arguments. To define a variadic function, three dots (...) are used as the last or only parameter to indicate that the function has a variable number of arguments. The ... values may be put in a table.

function foo(a, b, ...)
    print(a + b)
    for i, v in ipairs({...}) do
        print(v)
    end
end

foo(1, 2, "hello", "world", "!")

Methods[]

In object-oriented programming, a method is a function that belongs to an object or a class. Lua allows methods to be defined within tables. In the Roblox's Lua API, many Roblox types have methods. Instances, for example, have methods which are defined by classes.

Stats = {Points = 0}

function Stats.AddPoints(a)
    Stats.Points = Stats.Points + a
end

The colon (:) can be used instead of the period (.) to define a method, which introduces a hidden self parameter to the method in place of the first parameter that can be used to refer to the table the method of which is called. Whether or not the colon is used, as long as a certain parameter is used correctly, the methods can use the local variable of the parameter as a reference to the table rather than using a global variable.

Stats = {Points = 0}

function Stats:AddPoints(a)
    self.Points = self.Points + a
end
-- is equivalent to
function Stats.AddPoints(self, a)
    self.Points = self.Points + a
end

The colon is also used for a method call, which is a function call that passes the object the method of which being called as the first (hidden) argument.

Stats:AddPoints(10)
-- is equivalent to
Stats.AddPoints(Stats, 10)

Type system[]

In Luau, function parameters and return values can be given annotations using ":", along with local variables. However, the annotations do not work in the older Lua language where it will instead result in a syntax error.

--!strict

function foo(a : number, b : number, c : number) : number
    print(a, b, c)
    return a + b + c
end

local var : number
var = foo(5, 3, 1)

First-class functions[]

Lua has first-class functions, meaning functions in Lua can be passed as arguments, returned as values, and assigned to a variable or stored in a table. The Roblox engine's Lua API is designed with first-class functions in mind, with classes that use callback functions include BindableFunction light iconBindableFunction dark iconBindableFunction and classes that use event handlers include most classes in the API.

Callback functions[]

A callback is a function that is passed as an argument to another block of code, where it is expected to call back the function. Return values may be provided unlike event handlers. Some functions such as spawn and delay take callbacks as parameters.

spawn(foo)

In the Roblox engine, there are a few classes that have callback members where the function assigned is used as a callback. BindableFunction light iconBindableFunction dark iconBindableFunction with the callback property BindableFunction light iconBindableFunction dark iconBindableFunction.OnInvoke and the method that executes the callback BindableFunction light iconBindableFunction dark iconBindableFunction:Invoke facilitates two-way communication of scripts:

-- script 1
function foo(a, b, c)
    print(a, b, c)
    return a + b + c
end

local bindable = Instance.new("BindableFunction")
bindable.OnInvoke = foo
bindable.Parent = workspace

-- script 2
local bindable = workspace:WaitForChild("BindableFunction")
local var = bindable:Invoke(1, 3, 5)

Event handlers[]

In the Roblox engine, classes have built-in events as members which are represented by the RBXScriptSignal data type. RBXScriptSignal has a Connect method which is called to assign a function as an event handler to execute whenever the built-in event fires, returning a RBXScriptConnection. Unlike callback functions, event handlers are not responsible for returning values, there can be multiple event handlers of the same event, and event handlers are primarily used for performing a certain task in response to a specific event.

-- script parented to a part
function foo()
    print("Touched")
end

script.Parent.Touched:Connect(foo)

Anonymous functions[]

Lua allows functions to be defined without a name and not to be assigned to a variable as well.

-- script parented to a part
script.Parent.Touched:Connect(function()
    print("Touched")
end)

External links[]

Advertisement