Need help with a Stamina/Posture bar problem

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()
1 Like

Try this! :smile:

local function recoverStamina()
	local currentTime = os.clock()
	if currentTime - lastDecreaseTime >= 3 then
		local timeDifference = currentTime - lastDecreaseTime - 3
		local staminaIncrease = math.floor(timeDifference / 0.1) -- Increase stamina every 0.1 seconds
		stamina.Value = math.min(stamina.Value + staminaIncrease, MAX_STAMINA)
	else
		stamina.Value = math.min(stamina.Value + 1, MAX_STAMINA)
	end
	updateStaminaBar()
end

1 Like

Didn’t work, result is same as before ones like this;

Basically no waiting for 3 seconds and going too fast etc unknown_2023.06.22-19.46

External Media

It’s not working because whoever that is, they are using some sort of AI. I’ll figure it out, give me a few.

Im not using any type of AI, I’m just copy and pasting off past projects :slight_smile:

I did, I managed to fix it by comparing the new value to old value

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.