Help with Sprinting Script?

Hi Devs, Whenever I use this sprinting script and I hit 0 it keeps me at the max sprinting speed and doesn’t slow me down. Anybody know how to fix this?

Local Script:

local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local playerGui = plr.PlayerGui

local stamina = 100
local maxStamina = 100

local speedDif = 8
local drainRate = 20
local refreshRate = 10
local staminaRefresh = 1

local sprintHeld = false
local sprinting = false
local exhausted = false

local function sprint(active)
	if hum.MoveDirection.Magnitude > 0 then
	if exhausted then return end
	
	hum.WalkSpeed = active and hum.WalkSpeed + speedDif or hum.WalkSpeed - speedDif
	sprinting = active
	end
end

local function onInput(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and input.UserInputType ~= Enum.UserInputType.Gamepad1 then
		sprintHeld = input.UserInputState == Enum.UserInputState.Begin
		sprint(sprintHeld)
	end
end


local function updateStaminaUI()
	playerGui.StaminaGUI.Holder.StaminaHolder.StaminaFrame.StaminaBar.Size = UDim2.new(math.clamp(stamina / maxStamina, 0, 1), 0, 1, 0)
	playerGui.StaminaGUI.Holder.StaminaHolder.StaminaFrame.Percent.Text = tostring(math.floor(stamina)) .. "/" .. tostring(math.floor(maxStamina))
end

uis.InputBegan:Connect(onInput)
uis.InputEnded:Connect(onInput)

rs.Heartbeat:Connect(function(deltaTime)
	if sprinting then
		stamina = math.max(0, stamina - drainRate * deltaTime)
		updateStaminaUI()
		if stamina == 0 then
			sprint(false)
			exhausted = true
		end
	else
		stamina = math.min(100, stamina + refreshRate * deltaTime)
		if stamina >= staminaRefresh then
			updateStaminaUI()
			exhausted = false
			if sprintHeld then
				sprint(true)
			end
		end
	end
end)```

it does slowdown but at same time high up WalkSpeed

it’s just stamina regening 0 to 1 repetedly if user continually runs

due to using RunService.Heartbeat
it’s switching sprint to true and false and same things to exhausted when stamina reachs 0 to 1 and user continually runs

basically you need make exhausted around start when stamina like 15 or 20
and stamina no longer regens if user continually runs in that state