Text stuck in an infinite loop

Hey devs,

So I’m having problems with my overhead title displaying their name and health. It works, but every time it says “In a safezone” it will go to “HP: inf/inf” and go back and fourth (kind of like a glitchy effect)

Here’s my code: (it’s a localscript)

local humanoid = script.Parent.Parent.Parent.Humanoid

while wait() do
	if script.Parent.Text == "HP: inf/inf" then
		script.Parent.Text = "In a safezone"
	else
		script.Parent.Text = "HP: " .. math.round(humanoid.Health) .. "/" .. math.round(humanoid.MaxHealth)
	end
end

Still needing help on this. I tried using the AI assistant, but it sucks, so it didn’t help.

I still need help, no replies.

script.Parent.Text = "HP: " .. math.round(humanoid.Health) .. "/" .. math.round(humanoid.MaxHealth)

^ The code under the else condition is being fired, which causes your first condition to fire as well, causing a feedback loop.

I don’t think you should be checking conditions for your text, since your Humanoid can offer you more accuracy because of it’s Health property.

Does your Humanoid health get set to inf when it’s in the safezone?

instead of a while loop use GetPropertyChangedSignal(“Text”):connect() on scripts parent.

Yes.

Not too sure on how to do that. Maybe a bit of help? I’ve not used it in this case before.

1 Like

Yes.

As @FroDev1002 said, you should use :GetPropertyChangedSignal() instead of a while loop since your text gets changed constantly even when you’re setting the same text.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local Character: Model = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid:GetPropertyChangedSignal("Health"):Connect(function() -- This method will listen for changes in the Humanoids' health.
    if Humanoid.Health > 100 then
        -- Set text to "In a safe zone."
    end
end)
1 Like

Ohhh yes thats the problem! Since you are using a while loop every time you are setting the text to in safe zone, that means the text is no longer HP: inf/inf, thus it then displays the health, which them means it is set to HP: inf/inf, and then the cycle repeats!

1 Like

Thank you, it worked! I have marked your reply as a solution.

1 Like

Anytime!

Also, you may have to worry about other problems in this code since it ONLY checks for the Humanoid’s health, not whether they are actually in the safe zone.

A player could regenerate their health overtime when they’re NOT in the safe zone, which will trigger the :GetPropertyChangedSignal() method.

You could try looking into Region3 and check if they’re within the vicinity of the safe zone.

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