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

Why wont this loop stop properly?

Asked by 9 years ago

So when you run you get faster until your 50 walkspeed. But for some reason the loop doesnt seem to stop properly and just keeps making me go faster

repeat wait() until game.Players.LocalPlayer.Character ~= nil

local human = game.Players.LocalPlayer.Character.Humanoid
local x = .1

human.Running:connect(function(speed)
    if speed > 0 then
    repeat
        wait()
        print ("loop")
        human.WalkSpeed = (2^x)
        x = x * 1.04
        human.WalkSpeed = human.WalkSpeed + 16
    until human.WalkSpeed >= 50
    else 
        x = .1
        human.WalkSpeed = 16
    end
end)

1 answer

Log in to vote
2
Answered by 9 years ago

The reason for this is that the .Running event fires REPEATEDLY for as long as the Humanoid is moving. It doesn't only fire when the player first starts moving as you might expect. And this is causing problems because the repeat loop is being run over and over every time the event fires. And the thing about repeat loops is that they iterate at least once before checking the condition you define in the until. They're really just while loops that happen once before even checking to see if the condition is met. Sort of like telling a kid they can eat candy if their chores are done, but they eat candy first, get their chores done, and then eat more candy.

The solution to this isn't exactly clear-cut, as it really depends on what you'd like to do with it. My personal suggestion is to leave the .Running event altogether. The only options I can think of that use that event are hacky and bad coding practice. My choice would be to use the Humanoid.StateChanged event. Then all you have to do is check that the second argument, which will be an Enum.HumanoidStateType value is equal to the value of `Enum.HumanoidStateType.Running', as the second argument contains the value of the new Humanoid state. The Humanoid state is representative of what the Humanoid is up to, and it can only have one state at a time. So this is probably one of the best ways to do this.

I've adapted your code to use just this method.

repeat wait() until game.Players.LocalPlayer.Character ~= nil

local human = game.Players.LocalPlayer.Character.Humanoid
local x = 0.1

human.StateChanged:connect(function(oldState, newState)
    if newState ~= Enum.HumanoidStateType.Running then
        x = 0.1
        human.WalkSpeed = 16
        return
    end

    while human.WalkSpeed < 50 and wait() do
        human.WalkSpeed = 16 + 2^x
        x = x * 1.04
    end

    human.WalkSpeed = 50
end)

You'll notice I also rearranged the code. By removing the "if...else...end" and changing to an "if then return end", I've made the code slightly less nested (tabbed in) and therefore more readable, without making it work any different or adding any code. I also changed your first line of human.WalkSpeed = (2^x) and combined it into the actual line that changes it as there is no real reason to separate them. Lua uses PEMDAS for its order of operations, so you can trust that exponents (the E in PEMDAS) will happen before the addition. And, last but not least, you'll notice the last line reads human.WalkSpeed = 50. This just makes sure that, after the loop, the WalkSpeed is capped at the appropriate value instead of possibly being something like 57.947923320523 or whatever might happen from your exponential acceleration.

Hope this helps!

1
i nominate this for a.... uhhhh.... grammy???? bubbaman73 143 — 9y
Ad

Answer this question