Roblox Studio error

stack overflow

What stack overflow means in Roblox Studio, the three things that cause it with the code that triggers each, and the fix for each one.

Updated 23 October 20263 min readErrorLuau

In short

Every function call takes a little room on the call stack until it returns. A function that calls itself with no stopping point, or two functions that keep calling each other, fill that room and Luau stops with stack overflow. Find the function that calls itself, and give it a condition that ends it, or turn it into a loop.

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.Respawn:4: stack overflow

Other forms of the same message:

  • '__index' chain too long; possible loop

The line in the message is where the stack ran out, which is inside the repeating function. The stack under the error repeats the same lines many times: those lines are the loop.

If you meant something to repeat forever, such as a game loop, use while true do ... task.wait() end instead of a function calling itself.

Causes and fixes

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

A function calls itself with no way to stop

Recursion is fine when each call gets closer to an end. A countdown that calls itself again without checking for zero never ends.

Breaks Script in ServerScriptService named Countdown
local function countdown(n)
	print(n)
	countdown(n - 1) -- no stopping point: stack overflow
end

countdown(10)

Add the stopping condition before the call. For a simple count, a loop is clearer and cannot overflow.

Fixed Script in ServerScriptService named Countdown
local function countdown(n: number)
	if n < 0 then
		return
	end
	print(n)
	countdown(n - 1)
end

countdown(10)

-- the same as a loop
for i = 10, 0, -1 do
	print(i)
end

A repeating task was written as a function calling itself

A round loop or a spawner that ends by calling itself again looks fine, because each round waits. But every call stays on the stack until the game ends, so the script fails after about 20,000 waves (the depth the Luau command line tool reached in our test). That can be hours into a server, which makes it hard to reproduce.

Breaks Script in ServerScriptService named Waves
local function spawnWave(wave)
	print("wave", wave)
	task.wait(1)
	spawnWave(wave + 1) -- grows the stack every wave
end

spawnWave(1)

Use a loop for repeating work. Each pass finishes before the next starts, so nothing builds up.

Fixed Script in ServerScriptService named Waves
local wave = 1
while true do
	print("wave", wave)
	task.wait(1)
	wave += 1
end

A metatable sends a lookup back to itself

In class-style code, __index pointing at the table itself (instead of the class) sends every missing key round in a circle. Luau stops it with '__index' chain too long; possible loop. A __index function that reads self[key] again gives a plain stack overflow.

Breaks Script in ServerScriptService named Pets
local Pet = {}

local pet = setmetatable({}, {})
getmetatable(pet).__index = pet -- points at itself, not at Pet

-- '__index' chain too long; possible loop
print(pet.Name)

Point __index at the class table, which holds the shared methods, and give each object its own fields.

Fixed Script in ServerScriptService named Pets
local Pet = {}
Pet.__index = Pet

function Pet.new(name: string)
	local self = setmetatable({}, Pet)
	self.Name = name
	return self
end

function Pet.greet(self: typeof(Pet.new("")))
	print("Hi, I am " .. self.Name)
end

local pet = Pet.new("Rex")
pet:greet()

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.Respawn:4: stack overflow

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. Open Output (Window menu, or the Script tab), clear it with Ctrl+K (⌘K on Mac), and press Play.
  2. Find the first red line. It starts with the script's place in the Explorer and the line number, such as ServerScriptService.Shop:12:.
  3. Click the red line. Studio opens that script with the cursor on the failing line.
  4. Read the lines between Stack Begin and Stack End under it: they list the calls that led there, newest first, so you can see which function called the broken one.
  5. Still not sure which value is wrong? Add print(typeof(x), x) just above the line, or set a breakpoint on it and read the values in the Watch window.

Questions

How deep can recursion go before stack overflow?

There is a fixed limit, and it is far beyond what normal code needs. If you hit it, the function is almost certainly calling itself forever rather than needing more room, so look for the missing stopping condition.

Is stack overflow the same as Script timeout?

No. Script timeout means a loop ran too long without yielding. Stack overflow means calls were nested too deep. Both usually come from code that never stops.

How do I find the function?

Click the error, then look at the stack lines under it in Output: the same function and line repeat over and over. That function is the one calling itself.

Can RoCode fix it?

Yes. Paste the Output line into RoCode. It finds the repeating call in the script in your open place and adds the stopping condition or turns it into a 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, 23 October 2026), and every block marked Breaks compiles and fails only when it runs. The message wording was checked by running the broken code with the Luau command line tool and against the message strings in Roblox Studio itself on 23 October 2026. A type check catches misspelt APIs and wrong types, not game logic, so playtest in Studio before you publish.

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.