Luau reference
A searchable cheat sheet of Luau syntax and common patterns. Each card links to the lesson that teaches the idea in full.
Hello, world
The classic first program. Every Luau script can call print.
Basics
print("Hello, Roblox!")Comments
Comments are notes for you (and future-you) — Luau ignores them.
Basics
-- A single-line comment
--[[
A multi-line
comment block
]]Statements
Semicolons are optional. One statement per line is the usual style.
Basics
local x = 5
print(x) -- no semicolon neededLocal variables
Declare values with local. Locals stay inside the block they live in.
Variables & types
local coins = 100
local name = "Ada"
local isReady = trueReassigning variables
Variables hold one value at a time — you can change what they hold.
Variables & types
local score = 0
score = score + 10 -- same variable, new value
print(score) --> 10The main types
Numbers, strings, booleans, and nil are the core building blocks.
Variables & types
local a = 5 -- number
local b = "hello" -- string
local c = true -- boolean
local d = nil -- "no value yet"Check a value's type
type() returns the name of a value's type as a string.
Variables & types
print(type("hi")) --> string
print(type(5)) --> number
print(type(true)) --> boolean
print(type(nil)) --> nilMultiple assignment
Assign several variables at once, and use it to swap values.
Variables & types
local x, y = 10, 20
x, y = y, x -- swaps them
print(x, y) --> 20 10String basics
Text in Luau is a string. Double or single quotes both work.
Strings
local greeting = "Hello"
local name = 'World' -- single quotes work too
print(greeting, name)Concatenation
Use .. to join strings together.
Strings
local full = "Hello" .. " " .. "World"
print(full) --> Hello WorldString interpolation
Backticks let you embed values directly inside a string.
Strings
local coins = 42
print(`You have {coins} coins!`) --> You have 42 coins!Common string helpers
The string library covers the day-to-day text operations.
Strings
print(string.upper("hi")) --> HI
print(string.sub("banana", 1, 3)) --> ban
print(string.find("hello", "ell")) --> 2 4
print(string.rep("ab", 3)) --> abababArithmetic
The usual math, plus floor division and remainder.
Operators
print(7 + 3) --> 10
print(7 - 3) --> 4
print(7 * 3) --> 21
print(7 / 3) --> 2.333...
print(7 // 3) --> 2 (floor division)
print(7 % 3) --> 1 (remainder)
print(2 ^ 3) --> 8 (power)Comparison
Compare values for equality and ordering. ~= means "not equal".
Operators
print(5 == 5) --> true
print(5 ~= 6) --> true
print(5 < 6) --> true
print(5 >= 5) --> true
print("a" < "b") --> trueBoolean logic
and, or, and not combine true/false conditions.
Operators
print(true and false) --> false
print(true or false) --> true
print(not true) --> falseCompound assignment
Shorthand for updating a variable with an operation.
Operators
local x = 10
x += 5 -- x is now 15
x *= 2 -- x is now 30
x %= 4 -- x is now 2if / elseif / else
Run different code depending on a condition.
Control flow
local health = 20
if health <= 0 then
print("Game over")
elseif health < 30 then
print("Low health")
else
print("Doing fine")
endwhile loop
Repeat while a condition stays true.
Control flow
local n = 3
while n > 0 do
print(n)
n -= 1
end
-- prints 3, 2, 1Numeric for loop
Count up (or down) over a range. A third number sets the step.
Control flow
for i = 1, 5 do
print(i) -- 1 2 3 4 5
end
for i = 1, 10, 2 do
print(i) -- 1 3 5 7 9
endbreak
Exit a loop early from inside it.
Control flow
local n = 0
while true do
n += 1
if n >= 10 then break end
end
print(n) --> 10Define & call
Bundle code into a named function, then call it with arguments.
Functions
local function greet(name)
print("Hello, " .. name)
end
greet("Ada") --> Hello, AdaReturn values
A function can hand a value back to whoever called it.
Functions
local function double(x)
return x * 2
end
local result = double(21)
print(result) --> 42Multiple returns
Return several values at once and capture them all.
Functions
local function minMax(a, b, c)
return math.min(a, b, c), math.max(a, b, c)
end
local lo, hi = minMax(3, 9, 5)
print(lo, hi) --> 3 9Anonymous functions
Functions are values too — pass them around as callbacks.
Functions
Varargs
... accepts any number of extra arguments.
Functions
local function sum(...)
local total = 0
for _, value in ipairs({ ... }) do
total += value
end
return total
end
print(sum(1, 2, 3, 4)) --> 10Create & index
Tables hold multiple values. Lists start at index 1; dictionaries use keys.
Tables
local fruits = { "apple", "banana", "cherry" }
print(fruits[1]) --> apple (indexes start at 1)
local player = { name = "Ada", level = 5 }
print(player.name) --> Ada
print(player["name"]) --> Ada (same thing)Iterate a dictionary
pairs() visits every key and value.
Tables
local stats = { coins = 100, xp = 250 }
for key, value in pairs(stats) do
print(key, value)
endIterate a list
ipairs() walks a list in order, 1 to #list.
Tables
local items = { "sword", "shield", "potion" }
for i, item in ipairs(items) do
print(i, item)
endTable helpers
table.insert and table.remove keep lists tidy.
Tables
local list = { 1, 2, 3 }
table.insert(list, 4) -- { 1, 2, 3, 4 }
table.remove(list, 1) -- { 2, 3, 4 }
print(#list) --> 3task.wait
Pause the current script for a number of seconds.
Tasks & timing
print("starting")
task.wait(1) -- wait 1 second
print("later!")task.spawn
Run a function without blocking the code after it.
Tasks & timing
task.spawn(function()
task.wait(1)
print("spawned, 1 second later")
end)
print("this prints immediately")warn
Print a message that stands out in the Output window.
Debugging
warn("Careful — this value is unexpected")Common mistakes
The two errors beginners hit most: = instead of == and a missing end.
Debugging
-- Using = instead of == in a condition:
if coins = 5 then end -- error! use ==
-- Forgetting then / end:
if true then
print("missing end")
-- error: Expected 'end' (to close 'if' at line 1)