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

WalkSpeed goes past the max?

Asked by 3 years ago

I have a script where the player's speed goes up by 1 every 2 seconds until it reaches 25

My problem is, It keeps on going after 25 and until I stop holding shift

local p = game.Players.LocalPlayer 
local char = p.Character 
if not p.Character then 
    repeat wait() until p.Character 
end

local human = char.Humanoid 
local anim = game.Workspace:WaitForChild("Animation")
local loadAnim = human:LoadAnimation(anim) 


local UIS = game:GetService("UserInputService")

local holdingShiftKey = false


UIS.InputBegan:Connect(function(inputObject) 

    if(inputObject.KeyCode==Enum.KeyCode.LeftShift)then
       holdingShiftKey = true


        while holdingShiftKey do
            loadAnim:Play()

           repeat wait(2)
    char.Humanoid.WalkSpeed += 1
        until char.Humanoid.WalkSpeed == 25 or not holdingShiftKey 

        end

    end
end)


UIS.InputEnded:Connect(function(inputObject)

    if(inputObject.KeyCode==Enum.KeyCode.LeftShift)then
                holdingShiftKey = false
        char.Humanoid.WalkSpeed = 16
        loadAnim:Stop()
    end
end)

Can anyone help me?

2 answers

Log in to vote
1
Answered by 3 years ago

You have two loops nested inside each other, which is fine in some cases, but strange when the two are 'while' and 'repeat until' loops.

Nested Loop: Loops that are within each other


while true do -- outer loop repeat -- inner loop until end

The nested loops are also why your WalkSpeed is not stopping when reaching 25 and I will explain why. To start, you need to understand how repeat until works. In basic terms, it repeatedly performs the code until a condition is met. The key point here is that it performs the code then checks the condition.

repeat
    print("This will print once")
until true
-- outputs "This will print once" one time --

Even though our condition is true, the code is still performed as the condition is checked after the code. This is why you are encountering your problem where the WalkSpeed continues to increase. After your WalkSpeed reaches 25, the repeat loop ends. But, since you have the while loop playing with the repeat until loop, you go back to the top of the while loop and the code after the repeat statement is then ran once in a loop because of the while loop it is inside.

My suggestion is to change how you are doing this entirely, since you have the possibility of having multiple loops running because of the repeat wait(2) (Creating a lot of loops is bad and hard to keep track of). I would have a loop outside of your events and instead have your events set a variable like holdingShift to true or false depending on if they are pressing the key down or not.

Hopefully I explained this clearly, let me know if you are confused!

0
Thanks for explaining this to me, I managed to fix it after seeing your answer TheNINJA152 85 — 3y
Ad
Log in to vote
0
Answered by
Padugz 40
3 years ago

Maybe put a conditional statement under 'while holdingShiftKey do' loop. Put:

if Walkspeed == 25 or not holdingShift Key  then
break
end

Answer this question