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

While loop not running even when condition is true?

Asked by 5 years ago

I am trying to use a while loop for my script that is supposed to deplete the stamina bar for my run script (yes i know i can use toolbox, but i like making things myself). When I start running, the while loop is supposed to start and stop when I stop running, though it doesn't do anything. While loops I'm talking about: (also it's in local script)

while wait(0.05) and depleteStamina do
    if stamina == 0 then
        print("stamina0")
        depleteStamina = false
        script.Parent.Humanoid.WalkSpeed = 10
    else
        print("staminaD")
        stamina = stamina - 1
    end
    print(stamina)

    staminaBar.Size = UDim2.new(staminaBarMW / (mstamina / stamina), 0, staminaBar.Size.Y.Scale, 0)
end

while wait(0.25) and not depleteStamina do
    if stamina <= 0 then
        wait(0.75)
        stamina = 1
    elseif stamina ~= 100 then
        print("staminaR")
        stamina = stamina + 1
    end
    print(stamina)

    staminaBar.Size = UDim2.new(staminaBarMW / (mstamina / stamina), 0, staminaBar.Size.Y.Scale, 0)
end

run part of the script if needed:

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift and stamina > 0 then
        script.Parent.Humanoid.WalkSpeed = 16
        depleteStamina = true
        print("run ", depleteStamina)
    end
end)

UIS.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
        script.Parent.Humanoid.WalkSpeed = 10
        depleteStamina = false
        print("no run ", depleteStamina)
    end
end)

I have tried moving the wait(0.05) part outside of the condition statement, putting prints to detect if it actually did go into the loop (it didn't) and using breakpoints in the run part to debug and see if the depleteStamina variable was true (it was). I also tried removing the depleteStamina variable entirely from the condition statement (without putting it into the if statement), in which case it did work. I have also noted that on the regenerate stamina while loop, it works until I run and doesn't work even after I stop running. I have no idea what could be wrong, or if I missing anything. Help is appreciated.

1 answer

Log in to vote
1
Answered by 5 years ago

Alright so I think I see your issue...

What I think you can do to fix this.

Try adding some functions to connect these two. This is just a guess but try an IF statement for the while wait(0.05) and depleteStamina. It might fix the script, I'm not at a computer at the moment so I can't do much. Let me know if my idea works!

Ad

Answer this question