Stamina regeneration system not behaving as it should

  1. What do you want to achieve? Keep it simple and clear!
    A stamina system that after waiting 2 seconds it regenerates stamina to max.

  2. 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.

  3. 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.

if the function has no arguements you can just do spawn(regenerateValue)

also you probably need to use coroutines for the regeneration and then yield it when plrStamina.Changed is fired

Adding onto the above reply, the correct syntax for this is task.spawn, not spawn same as wait, it’s task.wait.

Now, you should bind this function to an RBXScriptSignal which would be served with an attribute changed for the humanoid in question. An example use case of this is as follows:

-- this is assuming you have stamina as an attribute
local Humanoid = path.to.humanoid

Humanoid:GetAttributeChangedSignal("Stamina"):Connect(function()
-- code
end)

Ok so I actually just fixed it myself.

Doing the function every time the value changes was a bad idea and doesn’t work with what I was trying to achieve. I appreciate all the help given despite my own vital mistakes lol. :expressionless:

In case anyone is wondering how, I solved it by instead making the code run whenever stamina changes through an event that would be fired if for example a player rolled; dashed; etc. Then check if it is currently regening, if not then wait 2 seconds and check again, if not then regenerate like normal. otherwise if it was still regening, disable all running regenerations, wait 2 seconds and then run the regenerate function again.


All code here is ran when the stamina event is fired.

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