Syntax
local lessons = 3
local published = false
local note: string? = nilExamples
Observe shadowing
Standalone Luau or a Script in ServerScriptService. Output is 8, then 3: the inner declaration creates another binding instead of updating the outer one.
local lessons = 3
do
local lessons = 8
print(lessons)
end
print(lessons)Handle an absent value
An optional result must be checked before using string operations. An empty string is still present and truthy; it does not mean nil.
local function label(value: string?): string
if value == nil then
return "Untitled"
end
return value
end
print(label(nil)) -- Untitled
print(label("")) -- empty lineBest practices
- Prefer local bindings; accidental globals can hide misspelled names.
- Use explicit nil checks when false, zero or an empty string are legitimate inputs.
- Choose different names for outer and inner values unless shadowing makes the code clearer.
At a glance
- Purpose
- Typed scripting and Roblox development
- File extension
- .luau
- Runs in
- Luau host; Roblox engine examples require Roblox Studio
- Usually used with
- Roblox APIs and Studio
Specifications & further reading
Related Luau documentation
Inference, annotations and strict mode
Luau can infer types from expressions, while annotations document the boundary of a function or record. Strict mode reports more incomplete assumptions. Static checks help authors; runtime checks still protect values received from players or saved storage.Conditionals and operators
Use if statements to choose actions and if expressions to choose values. Only nil and false are falsey. A condition like if count does not test whether a count is positive, because zero is truthy.Closures and captured state
A closure is a function that keeps access to locals from its surrounding scope. Each call to a factory can create independent state. Capturing a value also extends how long it stays reachable, which matters for long-lived callbacks.Luau and Roblox scripting
Start with Luau values and functions, then use Roblox Studio to connect that code to players, Instances and events. Luau is the language; Roblox supplies the game engine APIs. Codectionary supports editing and highlighting, while these Roblox examples run in Studio.
Luau can infer types from expressions, while annotations document the boundary of a function or record. Strict mode reports more incomplete assumptions. Static checks help authors; runtime checks still protect values received from players or saved storage.Conditionals and operators
Use if statements to choose actions and if expressions to choose values. Only nil and false are falsey. A condition like if count does not test whether a count is positive, because zero is truthy.Closures and captured state
A closure is a function that keeps access to locals from its surrounding scope. Each call to a factory can create independent state. Capturing a value also extends how long it stays reachable, which matters for long-lived callbacks.Luau and Roblox scripting
Start with Luau values and functions, then use Roblox Studio to connect that code to players, Instances and events. Luau is the language; Roblox supplies the game engine APIs. Codectionary supports editing and highlighting, while these Roblox examples run in Studio.