Control structures are statements that manage the flow of Luau code execution. There are four main types of control structures:
- An if then else statement executes code only if a specified condition is true. The code execution doesn't repeat.
- A while loop executes code only if a specified condition is true, and repeats execution while the condition remains true.
- A repeat loop executes code, and repeats execution if the condition is true.
- A for loop executes code a set number of times depending on specified inputs.
The condition for if statements, while loops, and repeat loops can be any Luau expression or value. If a value isn't false or nil, then Luau evaluates it as true in conditional statements. Unlike other scripting languages, Luau considers both zero and the empty string as true.
If Statements
The basic if statement tests its condition. If the condition is true, then Luau executes the code between then and end.
You can use an elseif statement to test for additional conditions if the if condition is false. You can use an else statement to execute code if all if and elseif conditions fail. The elseif and else parts are both optional, but you can't use either without an initial if statement.
In a chain of if, elseif, and else conditions, Luau tests conditions from top to bottom, stops at the first true condition, and executes the code that follows it.
if 2 + 2 == 5 thenprint("Two plus two is five") -- Doesn't print because the condition is falseelseif 2 + 3 == 5 thenprint("Two plus three is five") -- Two plus three is fiveelseprint("All conditions failed") -- Doesn't print because the previous condition is trueend
While Loops
A while—do loop evaluates if a specified condition is true or false. If the condition is false or nil, then the loop ends, and Luau skips the code in the loop. If the condition is true, then Luau executes the code in the loop and repeats the process.
local timeRemaining = 10while timeRemaining > 0 doprint("Seconds remaining: " .. timeRemaining)task.wait(1)timeRemaining -= 1endprint("Timer reached zero!")--[[ Resulting output:Seconds remaining: 10Seconds remaining: 9Seconds remaining: 8Seconds remaining: 7Seconds remaining: 6Seconds remaining: 5Seconds remaining: 4Seconds remaining: 3Seconds remaining: 2Seconds remaining: 1Timer reached zero!]]
Infinite Loops
You can use a while—do loop to write infinite game loops by setting true as the condition.
while true doprint("Looping...")task.wait(0.5)end--[[ Resulting output:Looping...Looping...Looping...Looping......]]
Repeat Loops
The repeat—until loop repeats until a condition is true. The conditional test evaluates after the code block runs, so the code block always runs at least once. Unlike other languages, the Luau scope of a local variable declared inside a repeat—until loop includes the condition.
local currentGoblinCount = 18-- Spawn goblins up to a maximum of 25 in the gamerepeatspawnGoblin()currentGoblinCount += 1print("Current goblin count: " .. currentGoblinCount)until currentGoblinCount == 25print("Goblins repopulated!")--[[ Resulting output:Current goblin count: 19Current goblin count: 20Current goblin count: 21Current goblin count: 22Current goblin count: 23Current goblin count: 24Current goblin count: 25Goblins repopulated!]]
For Loops
A for loop executes code a set number of times, either based on a numerical counter or the number of items in a collection.
Numeric For Loops
A for—do loop determines the number of times to execute the loop using a counter. The loop is declared with a start value, end value, and optional increment.
Luau sets the counter equal to the start value, executes the code block in the for loop, then adds the increment to the counter. If the increment is positive, then the process repeats until the counter is equal to or greater than the end value. If the increment is negative, then the process repeats until the counter is equal to or less than the end value.
The optional increment defaults to 1. It doesn't need to be a whole number.
for counter = 1, 3 doprint(counter)end--[[ Resulting output:123]]for counter = 1, 6, 2 doprint(counter)end--[[ Resulting output:135]]for counter = 2, 0, -0.5 doprint(counter)end--[[ Resulting output:21.510.50]]
Generic For Loops
The generic for loop iterates over items in a collection rather than a sequence of numbers. With generic for loops, you can execute code for each item in the collection, and can easily use each item in the code.
For loops need a function, or iterator, to iterate over different types of collections. The global ipairs() returns an iterator for arrays, and the global pairs() returns an iterator for dictionaries. The string library provides string.gmatch() to iterate over strings.
Generalized Iteration
In Luau, you can iterate over a table using the in keyword directly on the table, instead of using an iterator function such as ipairs():
for i, v in {1, 2, 3, 4, 5} doprint(i, v)end
Generalized iteration also lets you use the __iter metamethod to create a custom iterator function. This contrived example iterates over an array in reverse order, from its last element to its first:
local myTable = {1, 2, 3, 4, 5}
myMetatable = {
__iter = function(t)
local i = #t + 1
return function()
i -= 1
if i > 0 then
return i, t[i]
end
end
end,
}
setmetatable(myTable, myMetatable)
for i, v in myTable do
print(i, v)
end
--[[ Resulting output:
5 5
4 4
3 3
2 2
1 1
]]
Arrays
The ipairs() function returns an iterator that iterates through numerical indices in a table and returns an index and value for each element. This makes it appropriate for arrays, where all indices are numeric.
local array = {"a", "b", "c", "d", "e"}for index, value in ipairs(array) doprint(index, value)end--[[ Resulting output:1 a2 b3 c4 d5 e]]
Dictionaries
The pairs() function returns an iterator that iterates through all indices (including numerical indices) in a table and returns a key and value for each entry in the dictionary. The order of traversing elements in a dictionary table is arbitrary. This makes it appropriate for iterating over dictionaries, where items are stored out of order with non-numeric indices.
local dictionary = {[1] = "a",["Hello"] = "b",[5] = "c",[true] = "d",["World"] = "f",[false] = "e"}for key, value in pairs(dictionary) doprint(key, value)end--[[ Resulting output:Hello btrue dfalse eWorld f5 c1 a]]
Control Keywords
Breaking Loops
To force a loop to end, use the break keyword. The following code sample shows how to break an infinite while—do loop.
local secondsElapsed = 0local timeout = 5while true dotask.wait(1)secondsElapsed += 1print("Seconds elapsed:", secondsElapsed)if secondsElapsed == timeout thenbreakendendprint("Five seconds elapsed. Time to move on!")--[[ Resulting output:12345Five seconds elapsed. Time to move on!]]
Continuing Loops
To force a loop to iterate and start again, use the continue keyword. A for loop will iterate the counter; while and repeat—until will check the loop condition before continuing. The following code sample gets all children of an Instance of a specific ClassName.
local function GetChildrenOfClass(parent: Instance, className: string): {Instance}
local children = {}
for _, child in parent:GetChildren() do
if child.ClassName ~= className then continue end -- Iterates the loop
table.insert(children, child)
end
return children
end