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
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)
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!
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.