Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Over Head health Gui for NPC not working?

I want the text label to show me the health of the NPC but when I go into the game and hit the NPC its health keeps switching to random numbers and sometimes the number won't be a whole number

My Script for the text label which is just a normal script:

humanthing = script.Parent.Parent.Parent.Parent.Humanoid
humanthing.HealthChanged:Connect(function(newhealth)
local maxhp = humanthing.MaxHealth

while wait() do
	script.Parent.Text = newhealth
end
end)

1 answer

✓ Accepted answer

You do not need a loop at all. The healthchanged function will change the label's text every time the health changes.

local humanoid = script.Parent.Parent.Parent.Parent.Humanoid
local label = script.Parent
label.Text = tostring(humanoid.Health) -- init

humanoid.HealthChanged:Connect(function(newhealth)
label.Text = tostring(newhealth)
end)

If you want the health to be shown beside the maxhealth (which is how some people like) then you would do this:

local humanoid = script.Parent.Parent.Parent.Parent.Humanoid
local label = script.Parent
label.Text = tostring(humanoid.Health.."/"..humanoid.MaxHealth) -- init

humanoid.HealthChanged:Connect(function(newhealth)
label.Text = tostring(newhealth.."/"..humanoid.MaxHealth)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.