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.
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