Roblox Studio syntax error

Expected 'then' when parsing if statement, got 'X'

What Expected 'then' when parsing if statement, 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

Every if and every elseif in Luau is written if condition then. Luau read the condition and expected then next; the word after got is what it found instead. got 'print' (or any other name) means then was left out, got '=' means a single = was used to compare, and got 'do' means the loop keyword was used by mistake.

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.Shop:4: Expected 'then' when parsing if statement, got 'print'

Other forms of the same message:

  • Expected 'then' when parsing if statement, got '='
  • Expected 'then' when parsing if statement, got 'do'
  • Expected 'then' when parsing if statement, got '{'

This is a syntax error: the whole script does not run, not only that line. Studio underlines the spot in red in the Script Editor, and the line number in Output is the line where Luau found the wrong word, which is often the line after the if when then is missing at the end of a line.

Loops use do and if statements use then. Mixing them up gives this error for an if, and Expected 'do' when parsing while loop for a loop.

Causes and fixes

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

then is missing at the end of the if line

Forgetting then is the most common cause, especially on an elseif. Luau keeps reading the condition onto the next line, and reports the first word there.

Breaks Script in ServerScriptService named Shop
local coins = 120

if coins >= 100
	print("You can buy the sword")
end

Add then after the condition, on every if and every elseif. else takes no then.

Fixed Script in ServerScriptService named Shop
local coins = 120

if coins >= 100 then
	print("You can buy the sword")
elseif coins >= 50 then
	print("You can buy the shield")
else
	print("Keep collecting coins")
end

A single = used to compare

= stores a value and == compares two values. Inside an if, Luau finishes the condition at x and then finds = where then should be.

Breaks Script in ServerScriptService named Levels
local level = 5

if level = 5 then
	print("Max level")
end

Use == to test if two things are equal, and ~= to test if they are different.

Fixed Script in ServerScriptService named Levels
local level = 5

if level == 5 then
	print("Max level")
elseif level ~= 1 then
	print("Keep going")
end

Studio may show a second message on the same line, Expected identifier when parsing expression. Fix the first one and the second goes away.

do written instead of then

Code moved out of a while or for loop, or written from habit in another language, can end the if line with do or a curly bracket.

Breaks LocalScript in StarterPlayerScripts named HealthWarning
local health = 20

if health < 25 do
	print("Low health!")
end

Replace do (or {) with then, and close the block with end, not }.

Fixed LocalScript in StarterPlayerScripts named HealthWarning
local health = 20

if health < 25 then
	print("Low health!")
end

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.Shop:4: Expected 'then' when parsing if statement, got 'print'

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 else need then?

No. Only if and elseif take then. else is written on its own.

What is the difference between = and == in Luau?

= puts a value in a variable or property. == asks whether two values are equal and gives true or false, which is what an if needs.

Why does the error point at the line after my if?

Because a condition can carry on over several lines. Luau only knows then is missing when it reaches a word that cannot be part of the condition, which is on the next line.

Can RoCode fix it?

Yes. Paste the Output line into RoCode. It reads the script in your open place and fixes the if statement, and any others in that script with the same mistake.

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.