Expected 'end' (to close 'function' at line 1), got <eof>in Roblox Luau: what it means and how to fix it

What it means

The script ended (<eof> means end of file) while a block was still open. Every function, if, for and while needs its own end, and one is missing. Studio often adds a hint such as "did you forget to close 'then' at line 2?" which points at the block to check.

The same error also shows up as:

  • Expected 'end' (to close 'then' at line 2), got <eof>
  • Expected 'end' (to close 'do' at line 1), got <eof>

The smallest script that triggers it

Triggers the errorline 4: error here
local function onTouch(hit)
  if hit.Parent then
    print(hit.Name)
end

The fix

Fixedline 4: the fix
local function onTouch(hit)
  if hit.Parent then
    print(hit.Name)
  end
end

The only end in the first script closed the if, which left the function open. Adding one more gives each block its own end.

Common causes

  1. An if, for or while inside the function has no end of its own.
  2. An end was deleted while editing or pasting code.
  3. A Connect(function() block is missing the end that goes before its closing bracket.

Practice this until it sticks

A Deep Dive on blocks, functions and end: 5-minute lessons, each with a quiz. What you miss comes back in the next module. One Deep Dive a day is free. No card.