Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does while loops have a code limit? Its very annoying and I don't know how to fix it.

Hey! I i was wondering why roblox has a while loop code limit, context:

So I am trying out a new concept game, like a boat simulator but with cars (like The Crew 2). So I was making a boat that sprays water (like irl) when you drive it, so around the middle of it sprays water out and the rear engines sprays water out then goes back into the water. So, i typed this code

while true do
	if script.Parent.Parent.DriverSeat.Throttle == 1 then
		script.Parent.water.Enabled = false
		wait(.001)
	end
end

But all i get in the output is my least favorite sentence that makes my skin crawl like ants under my skin tearing up my insides I hate it that much:

Script timeout: exhausted allowed execution time

so, like the "detective" i am I put a script into the workspace that reads:

while true do
	wait(.001)
		print("hello")
end

And, well:

hello (x346)

So why do i get an error when i put a lot of code into it? I have the same wait (.001) and I get different output sentences. So why? Why does this happen and why does this always happen to me, I gte so matoviated then I get

Script timeout: exhausted allowed execution time

Then I just want to go back on the xbox and do nothing all day because i cant fix the problem.

Please help me.

2 answers

✓ Accepted answer

If it wouldn't error, it would just crash your studio, which you don't want happening.

And on this script:

while true do
    if script.Parent.Parent.DriverSeat.Throttle == 1 then
        script.Parent.water.Enabled = false
        wait(.001)
    end
end

while true do isn't needed because the code will fire once true.

1 comment

0

wow, ok i did not know that, yea i dont want studio to crash. thanks that fixed A LOT of problems

The reason you're having a problem is because the wait function is inside the if-statement in your loop. Whenever script.Parent.Parent.DriverSeat.Throttle is not equal to 1, the loop isn't suspended because the wait function is never called, so the loop will run again without yielding, causing an infinite loop without suspension. To fix this, you can just move the wait function out of your if-statement:


while true do

	if script.Parent.Parent.DriverSeat.Throttle == 1 then
		script.Parent.water.Enabled = false
	end
	
	wait()

end

This discussion is preserved as it was. New answers, comments, and votes are closed.