Roblox Studio syntax error

Expected 'do' when parsing while loop, got 'X'

What Expected 'do' when parsing while loop, got 'X' means in Roblox Studio, the three things that cause it with the code that triggers each, and the fix for each one.

Updated 5 November 20263 min readSyntax errorLuauWritten by

In short

Loops in Luau are written while condition do and for ... do, and closed with end. Luau read the loop's condition or range and expected do; the word after got is what it found. got 'then' means the if keyword was used, and any other word means do was left out.

On this page
  1. What it means
  2. Causes and fixes
  3. Fix it with RoCode
  4. Find the line
  5. Questions
  6. Sources

What the message means

Output (example)
ServerScriptService.Spawner:3: Expected 'do' when parsing while loop, got 'then'

Other forms of the same message:

  • Expected 'do' when parsing for loop, got 'print'
  • Expected 'do' when parsing for loop, got 'then'
  • Expected ',' when parsing index range, got 'to'

It is a syntax error, so the whole script does not run. The line in Output is where Luau found the wrong word, which is the next line when do is missing at the end of the loop line.

do is for loops and then is for if. The reverse mistake, do on an if, gives Expected 'then' when parsing if statement.

Causes and fixes

Three things cause this error in practice. Each one below has the smallest code that triggers it, then the fix.

then written instead of do

while has a condition like if does, so it is easy to end it with then.

Breaks Script in ServerScriptService named Spawner
local enemiesLeft = 3

while enemiesLeft > 0 then
	enemiesLeft -= 1
end

End every while and for line with do.

Fixed Script in ServerScriptService named Spawner
local enemiesLeft = 3

while enemiesLeft > 0 do
	enemiesLeft -= 1
	task.wait(1)
end
print("wave cleared")

A while loop that runs for a long time needs a task.wait() inside it, or Studio stops the script with Script timeout: exhausted allowed execution time.

do is missing at the end of a for loop

Luau reads the loop header and then the next word. Without do, that word is the first line of the loop's body.

Breaks Script in ServerScriptService named ListPlayers
local Players = game:GetService("Players")

for _, player in Players:GetPlayers()
	print(player.Name)
end

Add do at the end of the for line.

Fixed Script in ServerScriptService named ListPlayers
local Players = game:GetService("Players")

for _, player in Players:GetPlayers() do
	print(player.Name)
end

for i = 1 to 10 (a range written with to)

Some languages write a counting loop with to. Luau uses a comma: for i = 1, 10 do. With to, Luau reports Expected ',' when parsing index range, got 'to'.

Breaks Script in ServerScriptService named Countdown
for i = 1 to 10 do
	print(i)
end

Write the start, the end and an optional step, separated by commas.

Fixed Script in ServerScriptService named Countdown
for i = 10, 1, -1 do
	print(i)
	task.wait(1)
end
print("Go!")

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.

For example: ServerScriptService.Spawner:3: Expected 'do' when parsing while loop, got 'then'

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

  1. A syntax error stops the whole script before its first line runs, so nothing in that script works, not just the broken line.
  2. Open the script. The Script Editor underlines the problem in red; hover over the underline to read the message.
  3. Open Script Analysis (View tab) to see every syntax error in every script, with its line number. Double-click one to jump to it.
  4. The line number in the message is where Luau gave up, which is often below the real mistake. The (to close ... at line N) part points at the line where the unclosed bracket or block starts.

Questions

Does a repeat loop need do?

No. repeat ... until condition has no do and no end. Only while and for loops use do.

How do I write a for loop in Luau?

Counting: for i = 1, 10 do ... end. Over a list or a table: for index, value in list do ... end. Both end the first line with do and the block with end.

Do I still need pairs and ipairs?

Not in Luau: for key, value in someTable do works on its own. pairs and ipairs still work too.

Can RoCode fix it?

Yes. Paste the Output line into RoCode. It reads the script in your open place and fixes the loop.

Sources and further reading

How the code was checked: every block marked Fixed passes Luau's strict type checker against Roblox's API definitions (luau-lsp, 5 November 2026), and every block marked Breaks compiles only to fail with the message on this page. The message wording was checked by compiling each broken block on this page with luau-compile, which uses the same Luau parser as Roblox Studio, on 5 November 2026. A type check catches misspelt APIs and wrong types, not game logic, so playtest in Studio before you publish.

More Roblox errors

All Roblox Studio errors

Stop hunting errors line by line.

RoCode reads the scripts in your open place, fixes the error where it happens and tells you what it changed. Free every day, no card required.