Syntax
for index = 1, 3 do
print(index)
endExamples
Skip without changing the collection
Standalone Luau or a Script in ServerScriptService. Only positive values contribute to the total. The input array remains unchanged.
local values = {4, -2, 7, 0}
local total = 0
for _, value in ipairs(values) do
if value <= 0 then
continue
end
total += value
end
print(total) -- 11Remove entries safely
Traverse backwards when removing array elements by index. Shifting a later element cannot make this loop skip an unvisited entry.
local values = {1, 2, 3, 4}
for index = #values, 1, -1 do
if values[index] % 2 == 0 then
table.remove(values, index)
end
end
print(table.concat(values, ", ")) -- 1, 3Best practices
- Do not assume pairs produces sorted keys.
- Use a bounded loop for a finite task, and disconnect event-driven work when its owner disappears.
- For continuous Roblox work, choose an appropriate RunService event or task delay rather than while true without yielding.
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
Tables, arrays and dictionaries
A Luau table can hold a sequence, a dictionary or a record. A sequence uses consecutive integer keys starting at one; named fields describe a record. Type annotations describe the intended shape to the checker, but do not copy tables or validate incoming data at runtime.task scheduling and RunService
Use task scheduling for delayed or deferred work, and RunService when a feature genuinely depends on simulation or rendering steps. A delay is a minimum scheduling interval rather than an exact clock. Avoid doing work every frame when an event can report the change.Conditionals and operators
Use if statements to choose actions and if expressions to choose values. Only nil and false are falsey. A condition like if count does not test whether a count is positive, because zero is truthy.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 Luau table can hold a sequence, a dictionary or a record. A sequence uses consecutive integer keys starting at one; named fields describe a record. Type annotations describe the intended shape to the checker, but do not copy tables or validate incoming data at runtime.task scheduling and RunService
Use task scheduling for delayed or deferred work, and RunService when a feature genuinely depends on simulation or rendering steps. A delay is a minimum scheduling interval rather than an exact clock. Avoid doing work every frame when an event can report the change.Conditionals and operators
Use if statements to choose actions and if expressions to choose values. Only nil and false are falsey. A condition like if count does not test whether a count is positive, because zero is truthy.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.