Syntax
local function greet(name: string?): string
return "Hello, " .. (name or "visitor")
endExamples
Default only when absent
Standalone Luau or a Script in ServerScriptService. Passing zero must keep zero, while omitting the argument uses one. The return annotation describes both results.
local function batchSize(requested: number?): (number, boolean)
local size = if requested == nil then 1 else requested
return size, size > 0
end
local size, usable = batchSize(0)
print(size, usable) -- 0, falseCollect variadic numbers
The ... parameter accepts any number of numbers. Here they are collected into a dense array, then summed. This technique is for non-nil numeric arguments, not preserving arbitrary argument lists with holes.
local function sum(...: number): number
local total = 0
for _, value in ipairs({...}) do
total += value
end
return total
end
print(sum(2, 3, 4)) -- 9Best practices
- Make defaults explicit inside the function.
- Capture all needed return values at the call site; assigning to one local discards the rest.
- Keep validation at public boundaries rather than repeating it in every internal helper.
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
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.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.ModuleScripts and exported types
A ModuleScript returns a value for require to use. Keep reusable calculations in modules and choose storage based on who may require them. Requiring the same module on the client and server does not create shared memory between those environments.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.
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.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.ModuleScripts and exported types
A ModuleScript returns a value for require to use. Keep reusable calculations in modules and choose storage based on who may require them. Requiring the same module on the client and server does not create shared memory between those environments.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.