While loop going faster and faster every time it runs?

Take a look at this because I’m going crazy:

REMOVED

This while loop is supposed to add 1 to a Value until it reached 100. When it does, another function that resets the value and kills the player is called.

It works fine, but for some god forsaken reason, the loops goes faster and faster when the player respawns after dying. Could someone tell me how to fix this, perhaps it is a bug? Help me becuase I’m banging my head to the wall right now.

In the video above, you can see how the player dies faster and faster (when you can see the red vignette and the frame changes for a split second the player dies)
Now that’s the video, but in the output, I can see the print statement print(HungerValue.Value) going crazy.

BY THE WAY: HungerUpdateTime is set to 0.2

I want:
-A solution to this issue

-Perhaps another way to do this. I’m worried while true do might lag the client because this loop MUST be running constantly for my hunger system and I don’t know a better way to do it rather than this while loop, so if you got another approach to this, please tell me.

Thanks in advance.

1 Like

1- When you call the hunger function that initiates the while loop, where is this script located?
2- If you are calling the hunger function every time the player dies/spawns, more and more while loops will be initiated therefore seeing the effect that the while loop is getting faster.
3- If #2 is true then break the while loop when the player dies or after you call the function that causes the player to die.
Example:

local function HungerFunc()
		while true do

			if HungerValue.Value > 0 and HungerValue.Value <= 25 then
				HungerFrame.StateLabel.Text = StatesModule.HungerState1
				StatesFrame.Parent.HintStateHunger.Text = StatesModule.HungerHint1
				
			elseif HungerValue.Value >= 25 and HungerValue.Value < 50 then
				HungerFrame.StateLabel.Text = StatesModule.HungerState2
				StatesFrame.Parent.HintStateHunger.Text = StatesModule.HungerHint2	
				
			elseif HungerValue.Value >= 50 and HungerValue.Value < 75 then
				HungerFrame.StateLabel.Text = StatesModule.HungerState3
				StatesFrame.Parent.HintStateHunger.Text = StatesModule.HungerHint3
				
			elseif HungerValue.Value >= 75 and HungerValue.Value < 100 then
				HungerFrame.StateLabel.Text = StatesModule.HungerState4
				StatesFrame.Parent.HintStateHunger.Text = StatesModule.HungerHint4
				
			elseif HungerValue.Value >= 100 then
				OnDeathReset()
                break -- Break the function after the "death" function or character "reset" function (if this is not the function that does that then place it in the correct spot, im just assuming.
			end

			HungerValue.Value += 1
			task.wait(HungerUpdateTime) -- Changed to task.wait for higher accuracy.
			print(HungerValue.Value)
		end
	end

1 Like
  1. The LocalScript is inside a ScreenGui, so at the time the function is called it is inside PlayerGui

  2. You were right about this

  3. I applied this “break” and everything works perfectly now

Thanks for the help, however:

-Perhaps another way to do this. I’m worried while true do might lag the client because this loop MUST be running constantly for my hunger system and I don’t know a better way to do it rather than this while loop, so if you got another approach to this, please tell me.

Do you have any tips for this? Or the while loop running constantly is okay?

If I can add a small fix that’d be setting

while true do

To

while wait() do

This ensures the while loop runs constantly but has a really small delay so that it doesn’t make the player lag

1 Like

It’s not recommended to use wait(), you should use task.wait(). It’s faster too.
Adding on to this, it can be a code smell if you replace the true in while true do with wait(). It would be best to put it inside the loop.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.