Luau Basics 2: Choices, Loops, and Functions
The short answer: Programs make decisions and repeat themselves. In Luau, if statements choose what to do, for and while loops repeat work, functions package code so you can run it whenever you call its name, and tables hold lists and labeled collections. Every example on this page was test-run before the page was written, and the output shown is the output it really produced.
Choices: if, elseif, else
An if statement runs its code only when its condition is true. Add elseif to test more conditions, and else for "none of the above." Luau tests the conditions from top to bottom and stops at the first one that is true.
local score = 85
if score >= 90 then
print("Gold star!")
elseif score >= 50 then
print("You passed. Nice work!")
else
print("Keep practicing!")
endWhat you should see:
The score is 85. It is not 90 or more, so the first branch is skipped. It is 50 or more, so the second branch runs — and the else never gets a turn. One thing that surprises newcomers: in Luau, only false and nil count as false in a condition. The number 0 and even an empty string "" count as true.
Loops that count: for
A numeric for loop runs a set number of times. You give it a start value, an end value, and — if you want — a step. The step defaults to 1, and it can be negative to count down.
for count = 1, 5 do
print("Count:", count)
end
for count = 5, 1, -1 do
print("Countdown:", count)
end
print("Blast off!")What you should see:
Notice print can take more than one value at a time, separated by commas, and it prints them side by side with a space between.
Loops with a condition: while
A while loop checks its condition first, then runs — and keeps running as long as the condition stays true. Something inside the loop has to change the condition, or the loop never ends.
local jumpsLeft = 3
while jumpsLeft > 0 do
print("Jump! Jumps left:", jumpsLeft)
jumpsLeft -= 1
end
print("Out of jumps.")What you should see:
The freeze warning, straight from Roblox's guide: a loop like while true do runs forever on purpose — games use it for things like cycling colors. But an endless loop must pause between rounds. Roblox's control-structures guide says it plainly: always include a delay such as task.wait() in an infinite loop — leaving it out freezes the game and crashes Studio. Page 5 of this series uses a while true loop the safe way.
Functions: name it once, use it anywhere
A function is a named block of code. You write it once, then call it by name whenever you need it. Values you hand to it are called parameters, and a function can hand a result back with return.
local function addNumbers(first, second)
return first + second
end
print(addNumbers(4, 5))
local function makeGreeting(name)
return "Hello, " .. name .. "!"
end
print(makeGreeting("Alex"))
local function splitCoins(totalCoins)
local half = totalCoins / 2
return half, half
end
local myHalf, yourHalf = splitCoins(10)
print(myHalf, yourHalf)What you should see:
addNumbers(4, 5)hands in 4 and 5; the function returns their sum, andprintshows it.makeGreetingbuilds a string out of its input and returns that.splitCoinsshows a Luau specialty: a function can return more than one value — here, two halves — and the caller catches them with two variables.- Two facts worth knowing from Roblox's functions guide: if you call a function with extra values, the extras are ignored; if you call it with too few, the missing parameters are simply
nil. Andreturnends the function on the spot — code after a return does not run.
Tables: lists and labeled boxes
A table is Luau's one and only collection. Written with curly braces, { }, it works two ways. As an array, it is a numbered list. As a dictionary, it is a set of labeled boxes. Almost everything in a Roblox game — inventories, settings, player data — is a table under the hood.
local colors = {"red", "green", "blue"}
print(colors[1])
print(colors[3])
print(#colors)
table.insert(colors, "purple")
print(colors[4])
for index, color in ipairs(colors) do
print(index, color)
end
local playerCard = {
name = "Alex",
coins = 25,
hasKey = true,
}
print(playerCard.name)
print(playerCard.coins)
playerCard.coins = playerCard.coins + 10
print(playerCard.coins)
playerCard.level = 2
print(playerCard.level)What you should see:
- Arrays count from 1, not 0. The first item in a Luau array is
colors[1]. This trips up everyone who has seen other languages — in Luau, 1 is first. #colorsis the array's length — how many items it holds.table.insertadds an item to the end of the array.- The
forloop withipairswalks the array in order, handing you each item's index and value. - In a dictionary like
playerCard, each value has a name instead of a number. Read them with a dot:playerCard.coins. You can change a value, and you can add a brand-new labeled box at any time — that's howlevelappeared.
This page is part of the Roblox Luau series:
1. Installing Roblox Studio ·
2. Your First Script ·
3. Luau Basics 1: Variables, Numbers, and Words ·
4. Luau Basics 2: Choices, Loops, and Functions ·
5. Making Things Happen: Events ·
6. Saving a Player's Progress ·
7. The Player Talks to the Server ·
8. Build a Whole Mini-Game ·
9. Stay Safe and Keep Your Game Yours
Sources (official only; read in full November 7, 2026):
- Roblox Creator Documentation, Control structures (create.roblox.com/docs) — if/elseif/else testing top to bottom; only false and nil count as false, while 0 and "" count as true; numeric for loops with start, end, and step; while loops; the warning that an infinite loop without a task.wait() delay freezes the game and crashes Studio; iterating arrays with ipairs.
- Roblox Creator Documentation, Functions — how a function is defined and called; extra arguments are ignored and missing ones are nil; return hands values back (including more than one) and ends the function.
- Roblox Creator Documentation, Tables — tables as arrays and dictionaries; 1-based indexing; the # length operator; table.insert; reading and writing dictionary values with dot syntax; adding new key-value pairs.
Every code example on this page was run on a Luau interpreter before this page was written, and the outputs shown are the outputs the runs produced. Checked November 7, 2026.