Need help with Stamina

Hey everyone,

im trying to get my stamina system to work. theres a little bug however. everytime im standing still and spam shift, my stamina regens way faster than it should.

I tried using a variable to check if it was already running and to not loop again, but that didn’t work (shown here)

				local isalreadyrunning = false
			-- Restore stamina
			while not Running and Stamina < StaminaMax and isalreadyrunning == false do
				isalreadyrunning = true
				Stamina += 1
				print(Stamina)
				wait(0.03)
				if Stamina >= StaminaMax and frame.BackgroundTransparency == 0 then
					makeTrs:Play()
					makeBGTrs:Play()
					isalreadyrunning = false
				end
				isalreadyrunning = false
			end

Any help is appreciated. Thanks!

1 Like

local isalreadyrunning = false

– Restore stamina
function restoreStamina()
if isalreadyrunning then return end
isalreadyrunning = true

task.spawn(function()
    while not Running and Stamina < StaminaMax do
        Stamina += 1
        print(Stamina)
        wait(0.03)

        if Stamina >= StaminaMax and frame.BackgroundTransparency == 0 then
            makeTrs:Play()
            makeBGTrs:Play()
        end
    end
    isalreadyrunning = false 
end)

end

1 Like

didnt work. has the exact same issue

Where’s the break? the loop will never stop, add this.

if something then
    break
end
1 Like

What is the exact issue? Is the Regen regenerating too fast, or do you want a regen delay? (Waiting a few seconds after the player stops sprinting to regen.)

i tried using a variable again and it worked. no clue why it didn’t the first time.