Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this while loop crash my game? [closed]

Asked by 10 years ago

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.

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?

2 answers

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

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()
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. adark 5487 — 10y
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. Spongocardo 1991 — 10y
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. Spongocardo 1991 — 10y
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. Spongocardo 1991 — 10y
View all comments (2 more)
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. Spongocardo 1991 — 10y
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. adark 5487 — 10y
Ad
Log in to vote
-3
Answered by
Relatch 550 Moderation Voter
10 years ago
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 

1
RenderStepped is smoother, actually. adark 5487 — 10y
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. Spongocardo 1991 — 10y
0
Jeez, no need for 2 thumbs down. Relatch 550 — 10y