I am trying to make a GUI display the players health

What is your problem?
I am trying to make a GUI display the players health. The code seems like it should work but it doesn’t. Am I doing something wrong? Is there something I need to change in the code?

Here is the code (it is in a local script):

wait(3)

local healthDisplay = script.Parent
local frame = healthDisplay:WaitForChild(“Frame”)
local unit = frame.Size.X.Scale/100

local player = game.Players.LocalPlayer
local character = player.Character or player.Character:Wait()
local humanoid = character:WaitForChild(“Humanoid”)
local health = humanoid.Health

print(health)

health.Changed:Connect(function()
frame.Size = UDim2.new(unit*health,0,0.7,0)
print(frame.Size)
end)

I appreciate any replies

Move this to scripting support.

I looked for something like that. Where is it?

Nevermind. I got it thanks! It was right under my nose.

You’re really close to getting what you want! You just need to use the HealthChanged event of the humanoid as integers don’t have a Changed event. HealthChanged also returns the new hp the humanoid is at!

local healthDisplay = script.Parent
local frame = healthDisplay:WaitForChild("Frame")
local unit = frame.Size.X.Scale/100

local player = game.Players.LocalPlayer
local character = player.Character or player.Character:Wait()
local humanoid = character:WaitForChild("Humanoid")

print(humanoid.Health)

Humanoid.HealthChanged:Connect(function(newHp)
	frame.Size = UDim2.new(unit*newHp,0,0.7,0)
	print(frame.Size)
end)
2 Likes

Thank you so much for the reply. I will try this immediately. Thank you!

1 Like