#stamina not decreasing when sprinting

1 messages · Page 1 of 1 (latest)

exotic latch
#

im new to scripting and im making a script for a sprinting mechanic with stamina, the walkspeed does work for running and walking but my stamina is stuck at 99 for some reason.
here is the code



local myMaxStamina = 100 
local myStamina = myMaxStamina
local myWalkspeed = 10
local myRunspeed = 25

local AmSprinting = false
local AmGainStam = true
local AmExhaust = false

local UserInputService = game:GetService("UserInputService") 
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then 
        task.wait()
        local AmSprinting = true
        local AmGainStam = false
        Humanoid.WalkSpeed = myRunspeed
        while AmSprinting == true and myStamina > 0 do
            local myStamina = (myStamina - 1)
            print(myStamina)
            wait(0.1)
            if myStamina <= 0 then
                local AmSprinting = false
                local AmGainStamina = true
                local AmExhaust = true
                Humanoid.WalkSpeed = myWalkspeed
                wait(0.5)
                local AmExhaust = false
            end
            
        end
        
    end

end)

UserInputService.InputEnded:Connect(function(input) 
    if input.KeyCode == Enum.KeyCode.LeftShift then
        task.wait()
        local AmSprinting = false
        local AmGainStamina = true
        Humanoid.WalkSpeed = myWalkspeed
        if AmExhaust == true then
            repeat
                task.wait()
            until AmExhaust == false
        end
        
        while AmGainStamina == true and AmSprinting == false and AmExhaust == false do
            local myStamina = (myStamina + 1)
            wait(0.1)
            if myStamina == myMaxStamina then
                repeat
                    task.wait()
                    local AmGainStamina = false
                    Humanoid.WalkSpeed = myWalkspeed
                until AmSprinting == true
                
            end
        end
        
    end

end)
mortal ibex
#

Instead of

while AmSprinting == true and myStamina > 0 do
local myStamina = (myStamina - 1)
print(myStamina)
wait(0.1)

do

while AmSprinting == true and myStamina > 0 do
myStamina = (myStamina - 1)
print(myStamina)
wait(0.1)

elder jungle
#

thats a dumb move

south isle
#

The reason your stamina is stuck at 99 is a common mistake called Variable Shadowing. Inside your while loop, you have the line local myStamina = (myStamina - 1). By putting local there, you are creating a brand new variable that only exists inside that loop. The script subtracts 1 from 100, prints 99, and then that new variable is deleted when the loop restarts. The original myStamina at the top of your script stays at 100 forever. Remove the word local from inside your functions and loops. You want to change the variable you already created at the top, not make a new one.

exotic latch
#

Ohhh okeyy thank you guys for the help

exotic latch
slate pasture
#

its not a big deal, but its good practice to use task.wait instead of wait

exotic latch
#

Ohhh okey thank you

slate pasture
#

np!

mortal ibex
#

A ye I was just lazy