Stamina bar not working

Basically im working on a stamina bar but it doesnt work xd


local UserInputService = game:GetService("UserInputService")

local SPRINT_SPEED = 32
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
repeat wait() until player.Character
local char = player.Character
local hum = player.Character:WaitForChild("Humanoid")
local gui = script.Parent
local fill = gui.EnergyBar.Fill
local sprinting = false
local stamina = Instance.new("StringValue",player)
stamina.Name = "stamina"
stamina.Value = 100
local canSprint = true

UserInputService.InputBegan:connect(function(key, gameProcessedEvent)
	if not gameProcessedEvent and key.KeyCode == Enum.KeyCode.LeftShift and hum and not sprinting and canSprint then
		sprinting = true
		hum.WalkSpeed = SPRINT_SPEED
		while stamina.Value > 0 do
			fill:TweenSize(UDim2.new(stamina.Value * 0.01, 0, 1, 0), "Out", "Quad", 0.5)
			stamina.Value = stamina.Value - 1
			wait(0.5)
		end
		if hum then
			hum.WalkSpeed = 16
		end
		sprinting = false
	end
end)

UserInputService.InputEnded:connect(function(key, gameProcessedEvent)
	if not gameProcessedEvent and key.KeyCode == Enum.KeyCode.LeftShift and hum then
		sprinting = false		
	end
end)

while true do
	if stamina.Value < 100 then
		stamina.Value = stamina.Value + 1
		fill:TweenSize(UDim2.new(stamina.Value * 0.01, 0, 1, 0), "Out", "Quad", 0.5)
	end
	if stamina.Value >= 33 then
		canSprint = true
	elseif stamina.Value == 0 then
		canSprint = false	
	end
	wait(0.5)
end


1 Like

Which part of it doesn’t work? Does it not shorten the bar? Or does it keep going in reverse maybe?

Are there any errors in the output, or is it just not functioning?

I fixed it i had to use a intvalue xd

1 Like