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

For loop doesn't execute code after it finishes?

Asked by 5 years ago
Edited 5 years ago

So i've been ripping my hair out for the past 2 hours, but no matter what I do I just can't seem to fix this roadblocking bug.

function RocketGotoDestination(destination)
    for i=0, 5, 0.01 do
        wait(0.01)
        script.Parent.Rocket:SetPrimaryPartCFrame(script.Parent.Rocket.PrimaryPart.CFrame:Lerp(destination, i/5))
    end
    print("g")
end

As you can see, in this code, I have a function which runs a for loop and does a little animation using Lerp. However, in game, whenever the animation is supposed to stop, it does, however no code after it runs. Not even the "print("g")" code I put in for testing.

Any ideas on what could be the issue?

EDIT: so i've managed to fix the issue, which was that I didn't store the CFrame to be lerped into a variable. However, i'm still confused: this is ran in a for loop, not a while loop or whatnot. how come it wouldn't have just stopped then since eventually it would've reached "5" regardless?

0
Although it takes a bit longer after the part reaches destination, g is printed in output, unless it never prints you need to provide more code. DanzLua 2879 — 5y
0
wait(0.01) won't actually wait that long. wait()'s limit is 1/30th of a second. The fastest the server can wait is 1/40th of a second with RunService.Heartbeat:Wait(). PhantomVisual 992 — 5y

Answer this question