Codectionary / Developer documentation / Luau

Variables, values and scope

Use local names for values that belong to a function or block. A nested block can read outer locals, but a new local with the same name shadows the outer binding. nil represents absence; false is an actual boolean value.

Syntax

local lessons = 3
local published = false
local note: string? = nil

Examples

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 line

Best 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