stack overflowin Roblox Luau: what it means and how to fix it

What it means

A function kept calling itself, or two functions kept calling each other, until Luau ran out of room to track the calls.

The smallest script that triggers it

Triggers the errorline 3: error here
local function countdown(n)
  print(n)
  countdown(n - 1)
end
 
countdown(10)

The fix

Fixedline 2: the fix
local function countdown(n)
  if n < 0 then
    return
  end
  print(n)
  countdown(n - 1)
end
 
countdown(10)

The if gives the function a point where it stops calling itself, so the chain of calls ends.

Common causes

  1. A recursive function with no stopping condition.
  2. A Changed handler that sets the same property it listens to, which triggers itself again.
  3. An __index or __newindex metamethod that reads or writes the same table without rawget or rawset.

Practice this until it sticks

A Deep Dive on recursion and functions: 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.