Syntax
local count = 0
local function increment()
count += 1
endExamples
Create independent counters
Standalone Luau or a Script in ServerScriptService. Each factory call creates its own count. Calling a twice does not change b.
local function makeCounter(): () -> number
local count = 0
return function()
count += 1
return count
end
end
local a, b = makeCounter(), makeCounter()
print(a(), a(), b()) -- 1, 2, 1Capture a binding, not a frozen snapshot
Updating the outer local affects later calls. If a snapshot is needed, pass the value to a factory or store a separate local at the intended moment.
local multiplier = 2
local function scale(value: number): number
return value * multiplier
end
print(scale(3)) -- 6
multiplier = 4
print(scale(3)) -- 12Best practices
- Keep captured state small and give it a clear owner.
- Disconnect callbacks attached to long-lived services when their feature is removed.
- Avoid a single shared mutable module table when each player needs independent state.
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
Functions, optional arguments and returns
Functions group a calculation or action behind a named boundary. Optional types allow nil; they do not supply defaults. Return tuples and variadic arguments are useful when the number of values matters, but named record results are often easier to extend.Events, connections and cleanup
Connect registers a callback and returns a connection you can disconnect. The callback runs when the event fires, not when it is registered. Long-lived services can keep callbacks alive after a feature has ended, so give each connection a clear cleanup point.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.
Functions group a calculation or action behind a named boundary. Optional types allow nil; they do not supply defaults. Return tuples and variadic arguments are useful when the number of values matters, but named record results are often easier to extend.Events, connections and cleanup
Connect registers a callback and returns a connection you can disconnect. The callback runs when the event fires, not when it is registered. Long-lived services can keep callbacks alive after a feature has ended, so give each connection a clear cleanup point.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.