Answered Plainly
Display: Text size:

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!")
end

What you should see:

You passed. Nice work!

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:

Count: 1 Count: 2 Count: 3 Count: 4 Count: 5 Countdown: 5 Countdown: 4 Countdown: 3 Countdown: 2 Countdown: 1 Blast off!

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:

Jump! Jumps left: 3 Jump! Jumps left: 2 Jump! Jumps left: 1 Out of jumps.

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:

9 Hello, Alex! 5 5

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:

red blue 3 purple 1 red 2 green 3 blue 4 purple Alex 25 35 2

Sources (official only; read in full November 7, 2026):

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.