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.
Runtime & versions
Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Luau documentation