Stamina recovery problems

Player.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local Values = char:FindFirstChild("Values")
	local Stamina = Values:FindFirstChild("Stamina")
	local allowStaminaRegen = Values:FindFirstChild("allowStaminaRegen")
	local currStamina = Stamina.Value
	local prevStamina = Stamina.Value
	Stamina.Changed:Connect(function()
		currStamina = Stamina.Value
		if currStamina <= prevStamina then
			prevStamina = Stamina.Value
			if allowStaminaRegen.Value == true then
				allowStaminaRegen.Value = false
				wait(staminaRegenDelay)
				allowStaminaRegen.Value = true
			end
		end
	end)
end)

in theory the script should do what the few first time i used stamina did, after i use it, theres a delay, then stamina start to regen. When i start to spam it suddenly broke i think its because i used stamina at the same time the script enable stamina regen, I tried to fix it using this but it didnt work:

if allowStaminaRegen.Value == true then
	allowStaminaRegen.Value = false
	wait(staminaRegenDelay)
	allowStaminaRegen.Value = true
end

actually figured it out myself, the script gotta update previous stamina every time stamina changed not only every time the stamina got used

1 Like