Stamina Bar Script

Pretty much I have a stamina bar and sprinting script. The sprint is toggleable and when I leave it on and stand still the GUI makes it regenerate but Im pretty sure the stamina is either going down or not regenerating. I’m not 100% sure but this causes the bar and the actual stamina to not be synced. Im just curious if anyone knows whats wrong and how to fix it. Thanks!

Stamina GUI Script:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local staminaGui = player:WaitForChild("PlayerGui"):WaitForChild("Stamina")
local staminaBack = staminaGui:WaitForChild("StaminaBack")
local staminaFront = staminaBack:WaitForChild("StaminaFront")
local uiStroke = staminaBack:FindFirstChild("UIStroke")
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")

staminaFront.AnchorPoint = Vector2.new(0, 1)
staminaFront.Position = UDim2.new(0, 0, 1, 0)

staminaFront.BackgroundTransparency = 1
staminaBack.BackgroundTransparency = 1
if uiStroke then
	uiStroke.Transparency = 1
end

local maxStamina = 100
local currentStamina = maxStamina
local staminaDrain = 5
local staminaRegen = 5
local drainInterval = 1
local regenInterval = 1
local regenDelay = 3

local isSprinting = false
local isDraining = false
local isRegenerating = false
local uiFadedOut = true
local currentTween = nil

local regenerateStamina

local function isMovementKeyDown()
	return UserInputService:IsKeyDown(Enum.KeyCode.W) or 
		UserInputService:IsKeyDown(Enum.KeyCode.A) or 
		UserInputService:IsKeyDown(Enum.KeyCode.S) or 
		UserInputService:IsKeyDown(Enum.KeyCode.D)
end

local function updateStamina()
	local scale = currentStamina / maxStamina
	local goal = { Size = UDim2.new(1, 0, scale, 0) }
	local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
	if currentTween then
		currentTween:Cancel()
	end
	currentTween = TweenService:Create(staminaFront, tweenInfo, goal)
	currentTween:Play()
end

local function fadeInUI()
	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
	local tweenFront = TweenService:Create(staminaFront, tweenInfo, { BackgroundTransparency = 0 })
	tweenFront:Play()
	local tweenBack = TweenService:Create(staminaBack, tweenInfo, { BackgroundTransparency = 0.5 })
	tweenBack:Play()
	if uiStroke then
		local tweenStroke = TweenService:Create(uiStroke, tweenInfo, { Transparency = 0.5 })
		tweenStroke:Play()
	end
	uiFadedOut = false
end

local function fadeOutUI()
	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
	local tweenFront = TweenService:Create(staminaFront, tweenInfo, { BackgroundTransparency = 1 })
	tweenFront:Play()
	local tweenBack = TweenService:Create(staminaBack, tweenInfo, { BackgroundTransparency = 1 })
	tweenBack:Play()
	if uiStroke then
		local tweenStroke = TweenService:Create(uiStroke, tweenInfo, { Transparency = 1 })
		tweenStroke:Play()
	end
	uiFadedOut = true
end

local function drainStamina()
	if isDraining then return end
	isDraining = true
	spawn(function()
		while isSprinting and currentStamina > 0 do
			if isMovementKeyDown() then
				currentStamina = math.max(currentStamina - staminaDrain, 0)
				updateStamina()
			else
				break
			end
			wait(drainInterval)
		end
		isDraining = false
		if currentStamina <= 0 then
			isSprinting = false
		end
		if not isSprinting then
			regenerateStamina()
		end
	end)
end

regenerateStamina = function()
	if isRegenerating then return end
	isRegenerating = true
	spawn(function()
		wait(regenDelay)
		while not isSprinting and currentStamina < maxStamina do
			currentStamina = math.min(currentStamina + staminaRegen, maxStamina)
			updateStamina()
			wait(regenInterval)
		end
		isRegenerating = false
		if not isSprinting and currentStamina == maxStamina and not uiFadedOut then
			wait(3)
			if not isSprinting and currentStamina == maxStamina then
				fadeOutUI()
			end
		end
	end)
end

UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)

	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or 
		input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D then
		if not isMovementKeyDown() and isSprinting then
			isSprinting = false
			regenerateStamina()
		end
	end
end)

hum:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
	if hum.WalkSpeed > 16 then
		if not isSprinting and currentStamina > 0 then
			isSprinting = true
			if uiFadedOut then fadeInUI() end
			drainStamina()
		end
	else
		if isSprinting then
			isSprinting = false
			regenerateStamina()
		end
	end
end)

Sprint Script:

local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://100883967241908"
local track = animator:LoadAnimation(animation)
track.Priority = Enum.AnimationPriority.Movement

local RUN_KEYCODE = Enum.KeyCode.LeftShift
local RUN_SPEED = 24
local WALK_SPEED = 16

local isSprinting = false
local isMoving = false
local isInAir = false
local landingPause = false

local footStepsSound = char:WaitForChild("HumanoidRootPart"):WaitForChild("Running")

local maxStamina = 100
local currentStamina = maxStamina
local staminaDrain = 5
local staminaRegen = 5
local drainInterval = 1
local regenInterval = 1
local regenDelay = 3

local draining = false
local regenerating = false

local regenerateStamina

local function updateAnimation()
	if landingPause then return end
	if isSprinting and isMoving and not isInAir then
		hum.WalkSpeed = RUN_SPEED
		footStepsSound.PlaybackSpeed = 1.7
		if not track.IsPlaying then track:Play() end
	else
		hum.WalkSpeed = WALK_SPEED
		footStepsSound.PlaybackSpeed = 1.2
		track:Stop()
	end
end

local function drainStamina()
	if draining then return end
	draining = true
	spawn(function()
		while isSprinting and currentStamina > 0 do
			if isMoving then
				currentStamina = math.max(currentStamina - staminaDrain, 0)
				print("#"..currentStamina)
				if currentStamina == 0 then
					isSprinting = false
					updateAnimation()
					break
				end
			end
			wait(drainInterval)
		end
		draining = false
		if not isSprinting then
			regenerateStamina()
		end
	end)
end

regenerateStamina = function()
	if regenerating then return end
	regenerating = true
	spawn(function()
		wait(regenDelay)
		while not isSprinting and currentStamina < maxStamina do
			currentStamina = math.min(currentStamina + staminaRegen, maxStamina)
			print("#"..currentStamina)
			wait(regenInterval)
		end
		regenerating = false
	end)
end

local function inputBegan(input, processed)
	if processed then return end
	if input.KeyCode == RUN_KEYCODE and not isInAir then
		if isSprinting then
			isSprinting = false
			updateAnimation()
			regenerateStamina()
		else
			if currentStamina > 0 then
				isSprinting = true
				updateAnimation()
				drainStamina()
			end
		end
	end
end

local function floorMaterialChanged()
	if hum.FloorMaterial == Enum.Material.Air then
		isInAir = true
		track:Stop()
	else
		if isInAir then
			isInAir = false
			landingPause = true
			delay(0.25, function()
				landingPause = false
				if isMoving then
					updateAnimation()
				end
			end)
		else
			if isMoving and not track.IsPlaying then
				updateAnimation()
			end
		end
	end
end

local function moveDirectionChanged()
	isMoving = hum.MoveDirection.Magnitude > 0
	if not isInAir then updateAnimation() end
end

userInputService.InputBegan:Connect(inputBegan)
hum:GetPropertyChangedSignal("MoveDirection"):Connect(moveDirectionChanged)
hum:GetPropertyChangedSignal("FloorMaterial"):Connect(floorMaterialChanged)

Thanks again!

why don’t you store the current stamina as a number value in the player? you can just use a
stamina.Changed event to update the bar on the client and it will be a lot easier.