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.
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()
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
Locked by Spongocardo, kevinnight45, and adark
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?