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.
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.
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)```
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.
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 ```