In short
A counting loop, for i = start, limit, step do, needs three numbers (the step is optional). The error names the one that was wrong: initial value is start, limit is the end, step is the third value. got nil means that value was never set: an attribute that does not exist, a config key with a typo, or a function that returned nothing. Make sure it is a number before the loop starts.
initial value= the first number,limit= the second,step= the third.got tableusually means you wrote the table where you meant its length,#list.- To walk through a table, use
for i, item in list doinstead of counting.
What the message means
ServerScriptService.Waves:9: invalid 'for' initial value (number expected, got nil)
Other forms of the same message:
invalid 'for' limit (number expected, got nil)invalid 'for' step (number expected, got nil)invalid 'for' limit (number expected, got table)
Luau checks the three values once, when the loop starts. If any of them is not a number, the loop never runs and the script stops on the for line. A string that holds a number, such as "5", is converted and accepted; nil, a table or an Instance is not.
The type after got is what Luau found. nil is by far the most common, and it means the value you expected was never set.
Causes and fixes
Three things cause this error in practice. Each one below has the smallest code that triggers it, then the fix.
- A setting the loop counts to is missing
- The table was used instead of its length
- A function that should return a number returned nothing
A setting the loop counts to is missing
Loop limits often come from an attribute or a settings table. GetAttribute returns nil for an attribute that is not set, and a settings key with a typo is nil too.
local Settings = {
EnemiesPerWave = 5,
}
-- typo: the key is EnemiesPerWave, so the limit is nil
for i = 1, Settings.EnemiesPerwave do
print("spawn enemy", i)
end
Read the value into a local with a default, and give the settings table a type so the Script Editor flags the typo before you press Play.
type Settings = { EnemiesPerWave: number }
local Settings: Settings = {
EnemiesPerWave = 5,
}
local count = workspace:GetAttribute("EnemiesPerWave")
local enemies = if typeof(count) == "number" then count else Settings.EnemiesPerWave
for i = 1, enemies do
print("spawn enemy", i)
end
The table was used instead of its length
Counting up to a list needs the number of items, #list. Writing the list itself gives got table. If the list is nil, even #list fails, with attempt to get length of a nil value.
local spawns = workspace.Spawns:GetChildren()
-- invalid 'for' limit (number expected, got table)
for i = 1, spawns do
print(spawns[i].Name)
end
Loop over the table directly. You get each item without counting, and an empty table simply runs zero times.
local spawnFolder = workspace:WaitForChild("Spawns")
for i, spawnPart in spawnFolder:GetChildren() do
print(i, spawnPart.Name)
end
A function that should return a number returned nothing
A helper that forgets return, or returns early on one path, hands the loop nil. Here the start value is wrong, so the error says initial value.
local function getStartStage(player)
local stage = player:GetAttribute("Stage")
if stage then
return stage
end
-- new players: forgot to return 1
end
local function rebuildStages(player)
for stage = getStartStage(player), 10 do
print("build stage", stage)
end
end
Return a number on every path. A return type in the signature makes the Script Editor warn you when one path returns nothing.
local function getStartStage(player: Player): number
local stage = player:GetAttribute("Stage")
if typeof(stage) == "number" then
return stage
end
return 1
end
local function rebuildStages(player: Player)
for stage = getStartStage(player), 10 do
print("build stage", stage)
end
end
return rebuildStages
Fix it in your place
Paste the error, RoCode fixes it in your place
Copy the line from Output and paste it here. RoCode opens the script it names in the place you have open in Studio, works out which of the causes above it is, and edits the code there instead of handing you a snippet to paste.
RoCode is an AI agent for Roblox Studio: it reads the scripts in the place you have open, finds the line and edits it there, and every change is an undo point. It runs on Windows or macOS with a desktop browser. Free to start, with free credits every day.
How to find the line that causes it
- Open Output (Window menu, or the Script tab), clear it with Ctrl+K (⌘K on Mac), and press Play.
- Find the first red line. It starts with the script's place in the Explorer and the line number, such as
ServerScriptService.Shop:12:. - Click the red line. Studio opens that script with the cursor on the failing line.
- Read the lines between
Stack BeginandStack Endunder it: they list the calls that led there, newest first, so you can see which function called the broken one. - Still not sure which value is wrong? Add
print(typeof(x), x)just above the line, or set a breakpoint on it and read the values in the Watch window.
Questions
What is the initial value in a for loop?
The first number in for i = 1, 10 do, here 1. The second is the limit and the optional third is the step, such as -1 to count down.
Why does it work with "5" but not with nil?
Luau converts a string that holds a number into that number for the loop. nil, tables, booleans and Instances cannot be converted, so the loop refuses them.
How do I loop over every child of a folder?
Use for _, child in folder:GetChildren() do. No counting and no limit to get wrong.
Can RoCode fix it?
Yes. Paste the Output line into RoCode. It reads the script in your open place, finds where the start or limit comes from and fixes it.
Sources and further reading
- Roblox Creator Docs: control structures (for loops)
- Roblox Creator Docs: type coercion
- Roblox Creator Docs: Instance:GetAttribute
How the code was checked: every block marked Fixed passes Luau's strict type checker against Roblox's API definitions (luau-lsp, 14 November 2026), and every block marked Breaks compiles and fails only when it runs. The message wording was checked by running the broken code with the Luau command line tool and against the message format string in Roblox Studio itself (macOS build installed 12 November 2026) on 14 November 2026. A type check catches misspelt APIs and wrong types, not game logic, so playtest in Studio before you publish.