Skip To Content
Not affiliated with Roblox Corporation. 9Roblox never asks for your password and never offers free Robux.

Luau Loops Explained With Roblox Examples

Roblox Studio · Scripting Basics · Updated

Learn numeric for loops, generalized table iteration, while and repeat-until loops, break, continue, and task.wait() safety in Roblox Studio.

Difficulty
Beginner
Time Needed
15 To 20 Minutes
Loop Types
Numeric for, Generic for, while-do, repeat-until
Loop Control Keywords
break (Exit Loop) And continue (Next Iteration)
Modern Yield Call
task.wait() Inside Infinite Or Timed Loops
Table Iteration Shortcut
for key, value in myTable do (No pairs/ipairs Required)

Choose The Right Luau Loop Type

Loops let a Roblox script repeat work automatically—spawning rows of obstacle platforms, running round timers, or updating every player's attributes—without copying and pasting the same lines ten times. Luau supports four core loop styles: numeric for loops when the iteration count is known, generic for loops when scanning tables or Workspace children, while loops when repeating as long as a condition holds, and repeat-until loops when a block must run at least once before testing whether to stop.

Count Precisely With Numeric For Loops

A numeric for loop takes a start value, an end value, and an optional step increment: for count = 1, 10, 2 do ... end. When omitted, the step defaults to 1. The counter variable is scoped locally to the loop automatically, so writing local before it causes a syntax error.

Steps do not have to be positive whole numbers. Passing a negative increment counts backward—such as for countdown = 5, 1, -1 do—and fractional steps like -0.5 work cleanly for gradual transparency or lighting fades. Be careful when using negative steps: if the start value is already lower than the end value, the loop exits immediately without executing.

Walk Tables And Children Using Generalized Iteration

Classic Lua tutorials wrap tables inside ipairs() for sequential arrays and pairs() for dictionaries. Modern Luau keeps both functions for compatibility, yet also supports generalized iteration directly on any table without calling an iterator wrapper: writing for index, child in folder:GetChildren() do traverses a dense array in strict 1..#t order and visits any remaining dictionary keys cleanly.

Generalized iteration also supports custom table traversal through the __iter metamethod, letting advanced modules define reverse or filtered walks cleanly. Remember that dictionary key order remains arbitrary, so sort keys explicitly whenever deterministic ordering matters.

Run Timers And Retries With While And Repeat Loops

A while ... do loop checks its condition before every iteration, skipping the body completely if the initial check evaluates to false or nil. Whenever a script uses an open-ended while true do loop for a match timer or hazard cycle, include a yield such as task.wait(0.5) inside the loop; an unyielding infinite loop hogs the Luau thread and freezes or crashes Roblox Studio.

Whenever code only needs to react to a single change—like a player joining or health dropping—prefer event connections such as PlayerAdded or GetPropertyChangedSignal over a busy-waiting while loop.

A repeat ... until loop flips the test to the bottom of the block, running the body once first and stopping only when the until expression turns true. One subtle Luau feature sets repeat loops apart from many languages: any local variable declared inside the repeat body stays in scope inside the until condition.

Control Loop Flow With Break And Continue

Two keywords fine-tune execution inside any loop. Calling break exits the innermost enclosing loop immediately—useful for terminating a search once the target player or item is found. Calling continue (a Luau addition unavailable in stock Lua 5.1) skips the remaining lines of the current pass and jumps straight into the next iteration, advancing numeric counters or re-evaluating while and repeat conditions.

Watch out for one compiler rule in repeat ... until loops: continue cannot jump past the declaration of a local variable that the until condition reads, because that variable would be left uninitialized when the condition executes.

Build A Stepping Stone Round Script In Luau

Insert a Script into ServerScriptService and paste the self-contained Luau example below, then press F5 to test. Without any manual Workspace setup, the script combines a numeric for loop to create five anchored stepping stones, a repeat-until loop to pick a random bonus stone other than the first one, a while countdown using task.wait(1) and break, and generalized iteration with continue to highlight the chosen platform in gold.

local folder = Instance.new("Folder")
folder.Name = "SteppingStones"
folder.Parent = workspace

-- 1. Numeric for loop: build 5 evenly spaced anchored pads
for step = 1, 5 do
	local pad = Instance.new("Part")
	pad.Name = "Pad_" .. tostring(step)
	pad.Size = Vector3.new(6, 1, 6)
	pad.Position = Vector3.new(step * 10, 4, -20)
	pad.Anchored = true
	pad.Color = Color3.fromRGB(70, 150, 230)
	pad.Parent = folder
end

-- 2. Repeat-until loop: pick a bonus pad index guaranteed not to be 1
local bonusIndex: number
repeat
	bonusIndex = math.random(1, 5)
until bonusIndex ~= 1

-- 3. While countdown loop with task.wait() and break
local secondsLeft = 3
while secondsLeft > 0 do
	print("Round starts in:", secondsLeft)
	task.wait(1)
	secondsLeft -= 1
	if secondsLeft == 0 then
		break
	end
end

-- 4. Generalized for loop with continue over folder children
for index, item in folder:GetChildren() do
	if not item:IsA("BasePart") then
		continue
	end
	if index == bonusIndex then
		item.Color = Color3.fromRGB(245, 200, 55)
		print("Highlighted bonus platform:", item.Name)
	end
end

More In Scripting Basics