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

Why does this while loop crash my game?

Hi guys. I know that you need a wait inside a loop so that it doesn't crash but I have this script for a 2D morph I'm making below.

DECAL = script.Parent.Decal 
Decal2 = script.Parent.Decal2
humanoid = script.Parent.Parent.Parent:findFirstChild("Humanoid")
running = false
DecalId = "http://www.roblox.com/asset/?id=181415502"
DecalId2 = {"http://www.roblox.com/asset/?id=181415516", "http://www.roblox.com/asset/?id=181415520", "http://www.roblox.com/asset/?id=181415530", "http://www.roblox.com/asset/?id=181415537"}

function Run(speed)
	if speed > 0 then
		running = true
	else
		running = false
		DECAL.Texture = DecalId
		Decal2.Texture = DecalId
	end
end

humanoid.Running:connect(Run)

while true do 
	if running then
		for i,v in pairs(DecalId2) do
			DECAL.Texture = v
			Decal2.Texture = DECAL.Texture
			wait(0.075)
		end
	end
end 

If I don't add a wait() outside of the for loop in the while loop, my game crashes. But in a similar script, it doesn't crash with a wait() inside of the for loop with no wait() outside the for loop.

Now, if I add a wait() outside of the for loop and inside the while loop, my 2D morph's animations become choppy. I'm thinking of using RenderStepped in the RunService, but I don't see why it's necessary for me to do that.

Can anyone explain to me why this script is crashing my game and how I can fix it without a choppy animation?

Thanks in advance.

2 answers

✓ Accepted answer

An empty infinite loop that never wait()s crashes, and since running is presumably false in your example, that loop is effectively a wait()-less infinite loop.

To fix this, just add this line to the start of your loop:

game:GetService("RunService").RenderStepped:wait()

6 comments

1

As an addendum, you can use tick() in conjunction with RenderStepped to get a delta time and set your GUI stuff based on a fixed speed relative to the elapsed time.

1

But I have another script that has exactly the same format of the while loop I have, and that doesn't crash. I'll see about adding a "repeat wait() until running == true" if running is false. Running is a value that changes when the speed argument of the humanoid's Running event is over 0. I'll post my whole script, too.

1

Oh right, I'll try that. I just don't understand why the while loop makes the game crash when the for loop has a wait() in it, inside the while loop.

1

Ah, that wouldn't work. I should've mentioned that this is a server script. I'll try it as a LocalScript and see what happens.

1

Okay, that works. Thanks. I still don't understand why the wait() in the for loop would not stop the while loop from crashing my game, since other morphs I've made use the same while loop like this and it doesn't crash the game.

1

Think of it this way: when Lua's interpreter reads that loop, it will only run the code inside that if statement if running is true. When it isn't true, you can consider that entire if block to be nonexistant, and so you loop no longer has a wait in it.

while true do 
    if running then
        for i,v in pairs(DecalId2) do
            DECAL.Texture = v
            Decal2.Texture = DECAL.Texture
            wait(1/30) --1/30 is the smoothest wait time I know.
        end
    end
end 

3 comments

1

RenderStepped is smoother, actually.

1

I'm not looking to find the smoothest wait time, I'm looking for a way for the while loop to not crash while keeping a steady animation at the same, optimal wait time I have now. That wait time would be too fast.

0

Jeez, no need for 2 thumbs down.

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