So I’ve been trying to make a Sekiro like posture/stamina system and I’ve manage to make it partially work, but problem is stamina regen is every 3 seconds for a point, and I don’t want that, I want it to regen quickly after 3 seconds of no decrease of stamina
this is the issue in visual unknown_2023.06.22-21.40_2
the code
local player = game.Players.LocalPlayer
local playerGui = player.PlayerGui
local stamina = player.Data.Status.Stamina
local indicator = playerGui.PlayerUI.StaminaBar.Indicator
local bar = indicator.Bar
local lastDecreaseTime = os.clock()
local MAX_STAMINA = 100
stamina.Value = 100
local function updateStaminaBar()
local percentage = 1 - (stamina.Value / 100)
local maxBarSize = indicator.AbsoluteSize.X
local barSize = maxBarSize * percentage
bar.Size = UDim2.new(0, barSize, 1, 0)
bar.Position = UDim2.new(0.5 - barSize / (2 * maxBarSize), 0, 0, 0)
end
local function recoverStamina()
if lastDecreaseTime + 3 > os.clock() then
return
end
stamina.Value = math.min(stamina.Value + 1, MAX_STAMINA)
updateStaminaBar()
end
stamina.Changed:Connect(function()
if stamina.Value < MAX_STAMINA then
lastDecreaseTime = os.clock()
end
updateStaminaBar()
end)
game:GetService("RunService").Heartbeat:Connect(recoverStamina)
stamina.Changed:Connect(updateStaminaBar)
updateStaminaBar()