Why does a while loop with no yielding crash/timeout exactly?

while true do
	print("hey")
end

I know that you need to have yielding functions such as wait() inside a while loop, otherwise it’ll crash, but why does this crash exactly? I’ve tried this in C++ and vanilla lua (lua 5.1 from the Ubuntu repository) and it doesn’t, so what’s different with Roblox?

Other Lua programs still crash. Some Lua executers prevent this from happening by stopping the program or artificially slowing down the program.

The reason it crashes on Roblox is that there is no code telling it to wait, causing it to technically run an infinite number of times. The CPU gets overloaded, and you crash. Roblox does have measures to not crash forever, hence the error message saying the script timed out.

1 Like

Because it’ll hang the entire engine.

Basically what happens is that each script runs under a coroutine.

Once each coroutine yields or dies (finishes), it continues and calls the next. When you use wait(), you also yield but tell it only to resume this coroutine once the time is up, so it effectively sleeps the amount you specify.

But when you create an infinite loop with no yield, it will not resume any other scripts or any other parts of the engine and the engine will be stuck on your script until either:

  1. Your script ends/dies
  2. Your script yields
  3. The Luau VM determines that your script has hanged for too long and its coroutine is killed.
1 Like

It runs fine for me? Doesn’t seem to crash, just does what I expected; it runs as fast as it can.

Thanks, now that that think of it, if things run sequencially, such a script would indeed stop the whole engine.

1 Like

You can prolong the script execution time by specifically telling it to disable the timeout.

settings().Studio.ScriptTimeoutLength = -1

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.