Stamina bar is not working

I made an script for an Ui that is supposed to be an Stamina Bar , but it doesn’t work , when I am moving everything works fine , but when I stand still the Value that is the one controlling the bar , is instantly going to Value 100 , but the script says to go by 0.1 but its instant.

repeat wait() until game.Players.LocalPlayer.Character  
repeat wait() until game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").Died:Connect(function ()
	wait(game.Players.RespawnTime)
	game.ReplicatedStorage.Energy.Value = 100
end)
local db = false
while wait() do
	game:GetService("RunService").Heartbeat:Connect(function()
		if game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").MoveDirection.Magnitude > 0 then
			db = false
		else
			if game.ReplicatedStorage.Energy.Value <= 100 then
				wait(0.5)
				game.ReplicatedStorage.Energy.Value += 0.1
			end
			if game.ReplicatedStorage.Energy.Value > 100 then
				wait(0.1)
				game.ReplicatedStorage.Energy.Value = 100
			end
		end
	end)
	if db == false and game.Players.LocalPlayer.Character:FindFirstChild("Humanoid"):GetState() == Enum.HumanoidStateType.Running and game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").WalkSpeed == 16 then
		game.ReplicatedStorage.Energy.Value -= 0.1
		db = true
	end
	if game.ReplicatedStorage.Thirst.Value <= 10 or game.ReplicatedStorage.Hunger.Value <= 10 then
		game.ReplicatedStorage.Energy.Value -= 0.1
		db = true
	end
	if db == false and game.Players.LocalPlayer.Character:FindFirstChild("Humanoid"):GetState() == Enum.HumanoidStateType.Running and game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").WalkSpeed > 16 then
		game.ReplicatedStorage.Energy.Value -= 0.2
		db = true
	end
	script.Parent.Size = UDim2.new(game.ReplicatedStorage.Energy.Value/game.ReplicatedStorage.MaxEnergy.Value,0,1,0)
end
1 Like

RunService.Heartbeat fires every frame so those “wait” calls to yield the callback function connected to the event are essentially made redundant, just remove the heartbeat event and use a regular loop instead.