Like every other stamina feature in games, they regen after a certain amount of time. How could I achieve this?
Basically, there will be a countdown, if the stamina stops changing for 3 seconds then it will start regenerating, but if the stamina lowers while the countdown is ticking it will restart the countdown, how would I do this?
local val = script.Parent
local currentVal = val.Value
local isSprinting = script.Parent:WaitForChild("Sprinting")
local MAXIMUM_STAMINA = 100
while wait(.15) do
if val.Value < 100 and isSprinting.Value == false then
currentVal = val.Value
val.Value = val.Value +4
elseif val.Value > 100 then
val.Value = 100
currentVal = 100
end
end
you could use a bool value that turns true whenever it changes, and then once it turns true you can do wait(3) and then make it false, and you can do while bool == false do currentVal = CurrentVal + howevermuch stamina you want to add
Perhaps you can use a custom timer class which will do the timing for you. Then you can fire a signal when the timer has finished counting down to start an runservice connection to increase stamina every frame by a certain amount. Moreover you can even restart the count down of this timer class by setting the timer to 0 using the timers functions.
Moreover I would suggest using the time elapsed returned by the runservice step in order to make the stamina recharge not frame-rate dependent.
Thank you, I actually ended up using this script instead of the OOP though
local val = script.Parent
local currentVal = val.Value
local isSprinting = script.Parent:WaitForChild(“Sprinting”)
local runService = game:GetService(“RunService”)
local MAXIMUM_STAMINA = 100
local COUNTDOWN = 1.1
val:GetPropertyChangedSignal("Value"):Connect(function()
if val.Value < currentVal then
COUNTDOWN = 1.1
end
currentVal = val.Value
end)
while true do
runService.Stepped:Wait()
wait(.03)
if val.Value < 100 and isSprinting.Value == false then
if COUNTDOWN > 0 then
repeat
runService.Stepped:Wait()
wait(.1)
COUNTDOWN = COUNTDOWN -.1
print(COUNTDOWN)
until COUNTDOWN < .1
end
val.Value = val.Value +1
elseif val.Value > 100 then
val.Value = 100
end
end