Issue with stamina system

i was tryna make a basic stamina system unfortunatly whenever the running attribute is on it reduces stamina like intended however when the running attribute is off it still reduces the stamina, how would I fix this?


local player = game:GetService("Players").LocalPlayer
local stamina = player.playerstats.stamina
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local staminadrain = false
local chr = player.Character

chr:GetAttributeChangedSignal("Running"):Connect(function()
	local Running = chr:GetAttribute("Running")
	if chr:GetAttribute("Running") == true then
		while Running == true do
			stamina.Value -= 1
			task.wait(0.1)
		end
	end
	if chr:GetAttribute("Running") == false then
		task.wait(2)
		RunService.Heartbeat:Connect(function()
			if chr:GetAttribute("Running") == false then
				stamina.Value += 0.1
			end
		end)
		
	end
	
end)

stamina.Changed:connect(function()
	if stamina.Value > 100 then
		stamina.Value = 100
	end

	if stamina.Value < 0 then
		stamina.Value = 0
		if stamina.Value == 0 then
			chr:SetAttribute("CanRun", false)
			if stamina.Value == 20 then
				chr:SetAttribute("CanRun", true)
			end
		end
	end
end)

while Running == true do

this will always be true because Running only is defined in the beginning and you don’t redefine it before you check. use this line instead:

while chr:GetAttribute("Running") == true do

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.