Stumped on Stamina System

I am working on a stamina system. When a weapon is equipped naturally the stamina is checked and regenerates based on the amount of stamina a player has. This is done inside a coroutine amongst a bunch of other code.

The coroutine only runs when the tool is equipped. When it’s not equipped I’ve tried making functions and other coroutines to keep the value updating.

Lets say I have 10 stamina and decide to unequip my sword.
Stamina will now regenerate back to 100.
I decide to equip my sword again, and suddenly stamina goes back to 10.

local Stamina = 100

 --- This is the coroutine function that runs when the sword is equipped
coroutine.resume(coroutine.create(function()
	
	while true do	
		Stamina = updateStamina(0)
		local WaitTime = RunService.RenderStepped:Wait()
		if Equipped then
			if Stamina < 25 then
				updateStamina(8)
			elseif Stamina >= 25 and Stamina < 50 then
				updateStamina(6)
			elseif Stamina >= 50 and Stamina < 100 then
				updateStamina(4)
			end
--- this is my update function that ran between the coroutine above 
--- and any function i tried outside of the coroutine 
 function updateStamina(updateValue)
	local WaitTime = RunService.RenderStepped:Wait()
	Stamina = Stamina + WaitTime * updateValue
	return Stamina
end 

The only thing I could get to work but wasn’t practical was a tool.unequipped function. I would just give 10 stamina every time the tool was unequipped. Once I made it more complex by adding a while loop inside the function and giving stamina every few seconds, it stopped updating the values to the coroutine.

Sabre.Unequipped:Connect(function()
	Stamina = Stamina + 10
end)
2 Likes

Is this part of the script? It is out of the code section

2 Likes

Yes sorry for any confusion about that.

2 Likes

Are all of the scripts you posted chunks of 1 script, or are they each their own script, or some combination of both?

2 Likes

This is all one script. I only pasted the portions related to stamina as there is about 2000 lines of code in total.

2 Likes
local function updateStamina(updateValue, RunService, Stamina)
	local WaitTime = RunService.RenderStepped:Wait()
	Stamina = Stamina + WaitTime * updateValue
	return Stamina
end 

I think that you should put this function before the coroutine hopefully this works.

1 Like

No luck unfortunately. I tried this and nothing changed.

2 Likes

Do you have anything in errors in output?

2 Likes

No. The code just isn’t updating between coroutine and anything I try such as a while loop. I could run a while loop while the sword is unequipped, get to a stamina of 100. Equip the tool again and the stamina is back where I left off before I unequipped it.

At 20 Stamina
Unequips tool
At 100 Stamina (regens)
Equips tool again
Stamina back at 20

Sabre.Unequipped:connect(function()
	
	while Stamina < 100  do
		task.wait(1)
		Stamina = Stamina+1
		print(Stamina)
		Sabre.Equipped:connect(function()
			return Stamina
		end)
	end
	
	end)```
2 Likes

Is this your goal to get the tool working? It seems like the regen part is the only thing not working (correct me if I’m wrong). I would add a wait statement on the while true loop because I think that it is running too many times. To see if I’m right you could just add a print statement right after the while true do line.

1 Like

Okay so this function of code will regenerate stamina while the sword is unequipped.

	while Stamina < 100  do
		task.wait(1)
		updateStamina(100)
		print(Stamina, "dogs")
		Sabre.Equipped:connect(function()
			return Stamina
		end)
	end
	end)```

Now the output for the above reads the following:

31.037089582532644 dogs - Client - SabreLocal:80
15:09:13.021 Stamina: 32.80857967212796 dogs - Client - SabreLocal:80
15:09:14.054 Stamina: 34.39862968400121 dogs - Client - SabreLocal:80
15:09:15.070 Stamina: 36.02051975205541 dogs - Client - SabreLocal:80
15:09:16.087 Stamina: 37.658129800111055 dogs - Client - SabreLocal:80
15:09:17.104 Stamina: 39.17561981827021 dogs - Client - SabreLocal:80
15:09:18.121 Stamina: 40.71122983470559 dogs - Client - SabreLocal:80
15:09:19.137 Stamina:42.25119983777404 dogs - Client - SabreLocal:80
15:09:20.155 Stamina:44.05580980703235 dogs - Client - SabreLocal:80
15:09:21.187 Stamina:45.6679798476398 dogs - Client - SabreLocal:80
15:09:22.205 Stamina: 47.382859867066145 dogs - Client - SabreLocal:80
15:09:23.237 Stamina: 49.080799873918295 dogs - Client - SabreLocal:80

Okay so now we’re gonna look at the next line of code which occurs after I equip the tool again.

	
	while true do	
		Stamina = updateStamina(0)
		local WaitTime = RunService.RenderStepped:Wait()
		if Equipped then
			if Stamina < 25 then
				updateStamina(8)
			elseif Stamina >= 25 and Stamina < 50 then
				updateStamina(6)
			elseif Stamina >= 50 and Stamina < 100 then
				updateStamina(4)
			end	```

Now the output reads the following:
15:09:24.270 Stamina: 32.45931569300592 dogs - Client - SabreLocal:80
15:09:25.288 Stamina: 36.2252129111439 dogs - Client - SabreLocal:80
15:09:26.319 Stamina: 39.82151072099805 dogs - Client - SabreLocal:80
15:09:27.336 Stamina: 43.39643545076251 dogs - Client - SabreLocal:80
15:09:28.353 Stamina: 47.14880927093327 dogs - Client - SabreLocal:80
15:09:29.369 Stamina: 50.75294233299792 dogs - Client - SabreLocal:80

The problem is the stamina went backwards to 32. It’s like the coroutine isn’t updating the Stamina value when it starts back up? Idk Im so confused.

2 Likes