Health not updating with GUI

I’m trying to make a health GUI (without a health bar) but the health isn’t updating. It changes the first time when I load in (default text to the health), but after that it doesn’t update.

I haven’t really tried anything because I’m not sure what to try, I’ve tried looking up tutorials but all I’ve seen is for a health BAR and not actual numbers.

I’ve searched other DevForum posts but none of them worked, either that or I couldn’t really understand them (i am quite new to scripting).

Any help would be appreciated.

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local GUI = script.Parent
local Health = GUI.Health
local HP = Humanoid.Health

while true do
	wait(0)
	Health.Text = HP
end

You’re only updating it to a preset amount of health in the HP variable, you’re not updating it with the curernt hp of the Humanoid

I would recommend using .HealthChanged, which is an event of the Humanoid to determine the health of the humanoid rather tahn a while loop which is unperformant

This is a really simple misconception I’ve seen a lot of people do (including myself)

You’re storing the Humanoid’s Health property in a variable, which works. However, that will not update in realtime by itself to the value of the Humanoid’s Health property, this is what’s known as passing by value. The value of Health is stored in the variable, if that makes sense. So if the current health is at 100, HP will store a number being 100.

To fix? Just read from the Health property in your loop directly. However, it might be more efficient to use a Changed event to update the text, like so:

Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
Health.Text = Humanoid.Health
end)

There are 2 things I believe would be better with your example

HealthChanged, whilst completely the same as to how you did it, would be a better event to use as it also returns the current health of the humanoid

And also, due to how the health system in Roblox is made, it wont return an exact whole number for the hp, you’d have to floor it down

So something like this

Humanoid.HealthChanged:Connect(function(newhp)
	Health.Text = math.floor(newhp)
end)
1 Like

Thank you very much, I’ve genuinely been stuck on this for a few hours now.

1 Like

I would recommend flooring the Health of the humanoid since it’s going to return extreme decimals due to Roblox’s health system multiplying a wait with the health regeneration, causing non whole numbers

Ah, I already disabled the regen, but how exactly would I achieve this?

If you disabled the default regen system, then I don’t think there’s any need, but better to safe than sorry of course. I would recommend using the code I made which does exactly as @C_Sharper does, but floors the Health incase it’s a decimal number and uses .HealthChanged which is like how he did with GetPropertyChangedSignal but with HealthChanged, it also returns the current health of the humanoid, meaning we don’t have to reference the Humanoid’s health property

1 Like