Instant Regeneration Problem?

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!

Sounds great, let me know if it fixed your problem!

It did, and im extremely angry. Not at you of course, but my own stupidity. It took me 8 hours, 8 HOURS, for a fix that was as short as 2 minutes.

Thank you, but at the same time…

LOL! It happens to the best of us. Glad you figured out the problem!

1 Like

Why don’t you use a DoubleConstraintValue where you can set MinValue to 0 and MaxValue to whatever value you want?