-
What do you want to achieve? Keep it simple and clear!
A stamina system that after waiting 2 seconds it regenerates stamina to max. -
What is the issue? Include screenshots / videos if possible!
The stamina system does not behave as it should.
The issue I am having exactly is that my stamina system does indeed wait 2 seconds whenever a value changes, and then regenerates to max. However if the stamina value goes down again while it is waiting before regenerating, the 2 seconds of waiting isn’t accounted for the new value change and will regenerate right after. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Other posts and forums on this topic. Similar questions to my topic. and trying possible AI solutions to see if anything works.
Longer description
Basically I’m trying to create a stamina system that is handled server-sided to replenish stamina based of this behavior:
1: Wait 2 seconds once a value has changed.
2: Regenerate that value to max by 1 every 0.01 seconds
3: Every time the value changes WHILE the value is regenerating to max or is currently waiting, wait 2 seconds again, before regenerating again. Example: if it is waiting to be regenerated and the value changes whilst it is still waiting, restart the waiting process by waiting 2 seconds again then regenerate like usual.
Regeneration Stamina Server Code
local plrStamina = char:WaitForChild("Humanoid"):WaitForChild("Stamina")
local plrMaxStamina = char:WaitForChild("Humanoid"):WaitForChild("MaxStamina")
local regenerating = false
local changed = false
local previousValue = 0
-- Regen func
local function regenerateValue()
regenerating = true
while plrStamina.Value < plrMaxStamina.Value do
wait(0.2)
plrStamina.Value = plrStamina.Value + 1
if changed then
wait(2)
changed = false
end
if plrStamina.Value < 100 and regenerating == false then
wait(2)
if regenerating == false then
break
end
end
end
regenerating = false
changed = false
previousValue = 0
end
previousValue = plrStamina.Value
plrStamina.Changed:Connect(function(newValue)
if regenerating then
print(previousValue - newValue)
if (previousValue - newValue) > 8 then
changed = true
previousValue = plrStamina.Value
return
end
end
if not regenerating then
wait(2)
spawn(function()
regenerateValue()
end)
end
previousValue = plrStamina.Value
end)
This is the full code for the regeneration system. There is no errors during playtime.