GDScript Syntax Errors in Godot - Common Fixes (Solved)
Problem: Godot reports GDScript syntax errors such as "Parse Error: Expected ':'", "Unexpected token", "Invalid indentation", or "Identifier not found", and your script will not run or the scene will not load.
Quick Solution: GDScript is strict about indentation (tabs or spaces, not both), colons after block headers, and type hint syntax. Fix the line and column shown in the error, then re-run.
This guide covers the most common GDScript syntax errors in Godot 4 and how to fix them so you can get back to building your game.
The Problem: GDScript Syntax Errors in Godot
Common error messages:
- Parse Error: Expected
:after identifier - Parse Error: Invalid indentation
- Parse Error: Unexpected token
- Identifier not found:
variable_name - The global class "ClassName" doesn't exist
- Expected expression, found end of file
- Mismatched bracket or parenthesis
Why this happens:
- Indentation: GDScript uses indentation to define blocks (like Python). Mixing tabs and spaces or wrong indent level causes parse errors.
- Missing colons: Control flow and function/class definitions require a colon at the end of the line (
if,for,func,class, etc.). - Type hint syntax: Godot 4 uses
->for return types and:for variable/parameter types. Wrong placement or spelling causes errors. - Quotes and strings: Using the wrong quote type or unclosed strings breaks the parser.
- Reserved words: Using a keyword as a variable or function name.
- Brackets or parentheses: Unclosed or mismatched
(),[], or{}.
Solution 1: Fix Indentation
Indentation defines code blocks in GDScript. Use either tabs or spaces consistently; do not mix them.
Steps
- Check the error line: The bottom panel or Output will show the file and line (e.g.
res://script.gd:12). - Open the script and go to that line.
- Use one style: Choose either tabs or spaces for the whole file. In Godot Editor: Editor > Editor Settings > Text Editor > Indentation, set "Indent Type" to Tabs or Spaces and "Indent Size" (e.g. 4).
- Fix the block: Ensure everything under
if,for,func,class, etc. is indented one level more than the line with the colon. - Re-indent the file: Select all (Ctrl+A) and use the editor’s "Indent" / "Convert Indentation" so the whole file is consistent.
Example – wrong vs right:
# Wrong: no indent under if
if condition:
print("hello")
# Right:
if condition:
print("hello")
Verification: Save the script. If the only issue was indentation, the parse error goes away and the script runs.
Solution 2: Add Missing Colons
Every block-introducing line in GDScript must end with a colon.
Steps
- Find the line mentioned in the error (often "Expected ':'").
- Add a colon at the end of:
if,elif,elsefor,whilefunc,classmatchand eachmatchbranch
- Save and run again.
Example:
# Wrong
if health <= 0
die()
# Right
if health <= 0:
die()
Verification: The "Expected ':'" (or similar) message disappears after adding the colon.
Solution 3: Fix Type Hint Syntax (Godot 4)
Godot 4 GDScript uses type hints. Wrong syntax causes "Unexpected token" or "Invalid type".
Steps
- Variable type: Use a single colon and the type after the variable name.
- Correct:
var speed: int = 10 - Wrong:
var speed int = 10orvar speed: int: 10
- Correct:
- Return type: Use
->and the type before the colon of the function body.- Correct:
func get_health() -> int: - Wrong:
func get_health() -> int(missing colon) orfunc get_health(): -> int
- Correct:
- Parameter types: Type after the parameter name, before the comma or closing parenthesis.
- Correct:
func take_damage(amount: int): - Wrong:
func take_damage(int amount):
- Correct:
- Built-in types: Use the correct names:
int,float,String,bool,Array,Dictionary,Vector2,Node, etc. (case-sensitive).
Verification: After fixing type hint syntax, parse errors related to types should be gone.
Solution 4: Fix Quotes and Strings
Unclosed or mixed quotes break parsing.
Steps
- Strings: Use either
"double quotes"or'single quotes'consistently for a given string. Do not leave a quote unclosed. - Escaping: Inside double-quoted strings use
\"for a quote; inside single-quoted use\'. Or use the other quote type for the string. - Multiline strings: Use
"""triple double quotes"""and ensure the closing"""is on its own line or correctly placed.
Example:
# Wrong
var msg = "Hello
var msg = "She said "hi""
# Right
var msg = "Hello"
var msg = "She said \"hi\""
var msg = 'She said "hi"'
Verification: Save and run; "Unexpected token" or "Unclosed string" errors from quotes should be resolved.
Solution 5: Fix Identifiers and Reserved Words
"Identifier not found" or "Invalid identifier" often means a typo or use of a reserved word.
Steps
- Spelling: Check the exact name (GDScript is case-sensitive).
get_node()is notGet_Node(). - Reserved words: Do not use keywords as variable or function names. Examples:
class,func,if,else,for,while,return,var,const,and,or,not,in,true,false,null,self,signal,extends,enum,break,continue,pass,match,await. - Scope: Use
self.for member variables/functions if you are inside the same script and the name is ambiguous. - Node path: For
get_node(), use the path as in the Scene tree (e.g."Player/Weapon"). Wrong path gives runtime errors; typos in the string can cause "identifier" issues if you later use that node.
Verification: After renaming or fixing the reference, the identifier error should clear.
Solution 6: Fix Brackets and Parentheses
Mismatched or missing (), [], or {} cause parse or "expected expression" errors.
Steps
- Count: For every
(,[,{there must be a matching),],}. - Use the editor: Many editors highlight the matching bracket when you put the cursor on one. Use "Go to matching bracket" if available.
- Arrays and dictionaries:
[1, 2, 3]and{"key": "value"}must be closed. Trailing commas are allowed in GDScript 4. - Function calls: Ensure every
function_name(has a closing).
Verification: Once brackets and parentheses match, the parser can read the rest of the script correctly.
Alternative Fixes and Edge Cases
- Error on first line: If the error points to line 1, check the very first character (e.g. invisible character or BOM). Re-type the first line or save as UTF-8 without BOM.
- Error after adding a node/script: If you attached the wrong script or renamed the class, fix the script path or class name so it matches. "The global class doesn't exist" usually means the script is not loaded or the class name does not match.
- Only in one scene: If the same script works in another scene, compare the scene’s script attachment and any overrides. Ensure the script file is the one you edited.
- After upgrading Godot: Some deprecated or removed APIs cause errors. Check the Godot 4 migration guide and replace old syntax (e.g.
OS.window_fullscreenvsDisplayServer.window_get_mode()).
Prevention Tips
- Use the built-in script editor or an editor that supports GDScript (syntax highlighting and indent guides).
- Set Editor > Editor Settings > Text Editor > Indentation to Tabs or Spaces and stick to it.
- Enable "Draw Tabs" and "Draw Spaces" so you can see mixed indentation.
- Save often and run the scene after small changes so errors are easy to trace.
- Use type hints consistently; they catch many mistakes before run time.
- Keep functions short so indentation and structure stay clear.
Related Problems and Links
- If Godot itself crashes or freezes, see Godot 4.3 Editor Crashes When Opening Large Projects - Memory Fix.
- If the exported game shows a black screen, see Godot Black Screen After Export - How to Fix.
- If you see C# compilation errors in Unity instead, see Unity C# Compilation Errors - How to Fix.
- For Godot 4 scripting from scratch, see the Godot Game Development guide.
Bookmark this fix for quick reference when you hit GDScript syntax errors. If you are still stuck, copy the exact error message and line number and check the Godot documentation or community forums. Share this article with other Godot developers if it helped you.