Instant Regeneration Problem?

Hey, its prince once again,

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)

https://gyazo.com/acd986e416c154ea3af94b99995c7917
image

I cant seem to fix this, so as always any help/tips are much appreciated!

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)
1 Like

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

I dont want to be that person but you forgot a do
Also, using this didnt work at all unfortunately.

Also, i feel as if this isnt important but,
here is the GUI code for the stamina bar;

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Stamina = Character:WaitForChild("Stats"):WaitForChild("Stamina")
local MaxStamina = Character:WaitForChild("Stats"):WaitForChild("MaxStamina")
local Text = script.Parent.Parent.IndicatorS
local Bar = script.Parent

Text.Text = "Stamina: ".. Stamina.Value .."/".. MaxStamina.Value
Stamina.Changed:Connect(function()
	script.Parent.Size = UDim2.new(0,(Player.Character:WaitForChild("Stats"):WaitForChild("Stamina").Value/Player.Character:WaitForChild("Stats"):WaitForChild("MaxStamina").Value *592.9),.063, 0)
	Text.Text = "Stamina: ".. Stamina.Value .."/".. MaxStamina.Value
	print(1)
	print(Stamina.Value)
	
end)

MaxStamina.Changed:Connect(function()
	script.Parent.Size = UDim2.new(0,(Player.Character:WaitForChild("Stats"):WaitForChild("Stamina").Value/Player.Character:WaitForChild("Stats"):WaitForChild("MaxStamina").Value *592.9),.063, 0)
	Text.Text = "Stamina: ".. Stamina.Value .."/".. MaxStamina.Value
	print(1)
	print(Stamina.Value)
	
end)

Sadly - the code worked perfectly for me, :sweat_smile:. 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?

Of course;

Here it is:

https://gyazo.com/c49a4a199fa424813184172a5b3419c5

Studio is just harassing me at this point.

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 see. Any alternatives i should keep in mind while trying to solve this problem?

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:

if Player.Character ~= Character then break end
2 Likes

For further clearance, the problem is that it continues to go back to the original value and add.
EG;
image

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?

I tried what @Tomarty said. It also happens as soon as i join the game.

Ive also changed the code again to this:

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:

https://gyazo.com/19b96603c382cadbea6d942f78595022

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!