I have a stamina system in my game, and i want that per every second you gain 1 stamina, but if its greater than your maxstamina it auto becomes the max stamina
Heres the code:
local Players = game.Players
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local Stamina = Character:WaitForChild("Stats"):WaitForChild("Stamina")
local MaxStamina = Character:WaitForChild("Stats"):WaitForChild("MaxStamina")
while wait(1.45) do
Stamina.Value += 1
if Stamina.Value >= MaxStamina.Value then
Stamina.Value = MaxStamina.Value
print("Regenerating Stamina")
end
end
end)
end)
It is recommended that you avoid while wait() loops, the reasons to which can be found here.
You are better off using the heartbeat event from RunService, this is guaranteed to fire every frame.
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local NextStep = tick() + 1
RunService.Heartbeat:Connect(function()
if tick() >= NextStep then -- Every 1 second
NextStep = NextStep + 1
local allPlayers = Players:GetChildren()
for index = 1, #allPlayers() do
if workspace:FindFirstChild(allPlayers[index].Name) and not (workspace[allPlayers[index].Name].Stats.Stamina.Value >= Max) then -- Check the character exists
workspace[allPlayers[index].Name].Stats.Stamina.Value += 1
end
end
end
end)
I can’t seem to replicate the issue. The only thing I can think of is that the Stamina value is being updating all at once, but it doesn’t seem like that is the case.
Do you have some output messages that we can see to try to analyze the problem further?
Christ your lucky. Its not erroring at all and thats what seems to really bug me.
local Players = game.Players
local REGEN_INTERVAL = 2
local REGEN_AMMOUNT = 1
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local Stamina = Character:WaitForChild("Stats"):WaitForChild("Stamina")
local MaxStamina = Character:WaitForChild("Stats"):WaitForChild("MaxStamina")
local CurrentValue = Stamina.Value
while wait(REGEN_INTERVAL) do
if CurrentValue < MaxStamina.Value then
CurrentValue += REGEN_AMMOUNT
print("Regeneratiing Stamina")
wait()
if CurrentValue >= MaxStamina.Value then
CurrentValue = MaxStamina.Value
print("Stamina has been fully replinished")
end
end
end
end)
end)
Ive changed it to this, yet it still is instantly regenerating. Any corrections are appreciated
Sadly - the code worked perfectly for me, . The lack of errors does make it more difficult to troubleshoot.
Does there happen to be any other location that the Stamina value is being changed?
Edit* For less confusion - I removed the CurrentValue variable for more clarity, but it still seemed to work for me.
Here’s the portion I adjusted:
while wait(REGEN_INTERVAL) do
print(Stamina.Value) -- Check before anything is changed.
if Stamina.Value < MaxStamina.Value then
Stamina.Value += REGEN_AMMOUNT
print("Regenerating Stamina")
if Stamina.Value >= MaxStamina.Value then
Stamina.Value = MaxStamina.Value
print("Stamina has been fully replinished")
end
end
print(Stamina.Value .. " Done") -- Checks to see if there was any issues inbetween.
end
Unfortunately, ive checked and their are no possible places that the value could have been changed. Would you mind showing a gif of the IntValue changing?
This code will leak threads and cause lag as people join and leave the game, because it will continue looping even when the character is changed or the player leaves.
I generally avoid wait loops like that because they can’t be disconnected explicitly when the player leaves, but adding a check like this inside the loop should fix the leak:
From my understanding, there must be multiple instances of it running, on to what @Tomarty was saying about leaks (unless you fixed it already before posting). Have you tried his solution first before running it again?
Also, does this happen as soon as you start the game, or after your character has respawned a couple of times?
local Players = game.Players
local NextRegen = tick() + 1
local REGEN_AMMOUNT = 1
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local Stamina = Character:WaitForChild("Stats"):WaitForChild("Stamina")
local MaxStamina = Character:WaitForChild("Stats"):WaitForChild("MaxStamina")
if tick() >= NextRegen then
NextRegen = NextRegen + 1
while NextRegen do
wait(3)
Stamina.Value = Stamina.Value + REGEN_AMMOUNT
print("Regenerating")
if Stamina.Value >= MaxStamina.Value then
wait(1)
Stamina.Value = MaxStamina.Value
print("Fully Replinished")
end
if Character ~= Character then break end
end
end
end)
end)
The true problem isnt that the code isnt working. The problem is that the code is adding on from the orginal value, and not the changed value after i use a skill to decrease the stamina.
Ive included a rbxm with the script, the skill script(poorly made) and the character model.
Everything is inside of StarterCharacter.
Again, i appreciate all efforts to help me overcome this hurdle. Help would be appreciated.rbxm (16.4 KB)
I see, the only outcome I could think of is if the stamina script is on the server (which makes since if you change it client side), which gives me this question:
Are you changing the value on the client or server? If the script is server-sided (Which is seems to be), the client changing the value will not effect it; it must be done server side.
Just in case you’re not sure how to do it in studio:
I think what had happened was that, when i decreased the value for the cooldown, i did it in the local script. ill change it to be server sided. I did not think of this at all!