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.

6 comments
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.
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.
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.
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.
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.
Think of it this way: when Lua's interpreter reads that loop, it will only run the code inside that if statement if
runningis true. When it isn't true, you can consider that entireifblock to be nonexistant, and so you loop no longer has awaitin it.