i want to make a stamina bar that drains while running with tweening it but im not all that good with gui’s.
would anyone know how to implent this into my current code here is my code
local Stamina = 100
local SprintHeld = false
local Sprinting = false
local Exhausted = false
local RunRefresh = 20
local SpeedDiff = 6
local DrainRate = 20 -- drain per second
local UserInputService = game:GetService("UserInputService")
local Humanoid = game.Players.LocalPlayer.Character.Humanoid
local function sprint(active)
if Exhausted then return end -
if active then
Humanoid.WalkSpeed = Humanoid.WalkSpeed + SpeedDiff
else
Humanoid.WalkSpeed = Humanoid.WalkSpeed - SpeedDiff
end
Sprinting = active
end
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Z then
SprintHeld = true
sprint(SprintHeld)
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Z then
SprintHeld = false
sprint(SprintHeld)
end
end)
local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function(DeltaTime)
if Sprinting then
if Stamina > 0 then
Stamina = Stamina - DrainRate * DeltaTime
else
sprint(false)
Exhausted = true
end
elseif Stamina < 100 then
Stamina = Stamina + DrainRate * DeltaTime
if Stamina > RunRefresh then
Exhausted = false
if SprintHeld then
sprint(SprintHeld)
end
end
end
end)