hello all, so I have this very basic stamina script but one thing i’m confused about is after about 4 jumps the “staminatimer” or cooldown starts to speed up ridiculously fast. I tried fixing it with some debounces but I truly don’t know what im doing here
local maxStamina = 100
local debounce = true
local startingMoney = 500
local staminaTimer = 5
local function staminaHolder(player, staminaTimerPassed)
if staminaTimerPassed then staminaTimer = staminaTimerPassed end
if debounce then
debounce = false
while wait(1) do
if staminaTimer ~= 0 then
staminaTimer -=1
print(staminaTimer)
end
if staminaTimer == 0 then
debounce = true
end
while staminaTimer == 0 do
wait(.1)
if player.currentStamina.Value < maxStamina then
player.currentStamina.Value += 1
end
end
end
end
end
Replicated.playerJumped.OnServerEvent:Connect(function(player)
print("FINALLY")
local staminaTimer = 5
staminaHolder(player, staminaTimer)
The problem is that these loops continue to stack. Meaning that every time you run this function, the speed at which the timer ends and the stamina increases will continue to increase. I would make a separate loop not inside of any function, and it would be like this:
local maxStamina = 100
local debounce = true
local startingMoney = 500
local staminaTimer = 5
local recovering = false
while task.wait(1) do
if recovering then
if staminaTimer ~= 0 then
staminaTimer -=1
print(staminaTimer)
end
while staminaTimer == 0 do
task.wait(.1)
if player.currentStamina.Value < maxStamina then
player.currentStamina.Value += 1
end
end
end
while staminaTimer == 0 do
wait(.1)
if player.currentStamina.Value < maxStamina then
player.currentStamina.Value += 1
end
end
This loop will never break since when staminaTimer == 0 it will just endlessly do this loop. So every time this function is called there is always going to be this loop going. Call it four or five times and you have four or five loops changing the currentStamina.Value. Use Kaiden’s version and try this to break the inner loop.
while player.currentStamina.Value < maxStamina do
wait(.1)
player.currentStamina.Value += 1;
end
recovering = false;
When this is fired Replicated.playerJumped.OnServerEvent:Connect(function(player)) just set recovering to true.