My Health Displayer wont work Properly

Hello! So Im trying to make a Boss for my game The Long Jump, Im making a boss, So When I made the Health Bar in my own form this is how the script came out

local Humanoid = script.Parent.Parent.Parent.Parent.Humanoid
local Health = Humanoid.Health
local HealthText = script.Parent

while true do
	wait(1)
	HealthText.Text = "Health: "..Health
end

The Issue is when I changed the health of the boss the boss health Bar ( UI ) wont change

Theirs no errors

Try updating the “Health” variable each time the loop is iterated.

local Humanoid = script.Parent.Parent.Parent.Parent.Humanoid
local Health = Humanoid.Health
local HealthText = script.Parent

while true do
	wait(1)
	Health = Humanoid.Health
	HealthText.Text = "Health: "..Health
end
1 Like

Didn’t seem to work for it!


Instead of using a while do loop to detect when the health changes, you can just call the Humanoid.HealthChanged event, and then change the text.
Try using this code for your script:

local Humanoid = script.Parent.Parent.Parent.Parent.Humanoid
local HealthText = script.Parent

HealthText.Text = "Health: "..math.round(Humanoid.Health)

Humanoid.HealthChanged:Connect(function()
	HealthText.Text = "Health: "..math.round(Humanoid.Health)
end)

Humanoid.HealthChanged should be used instead of a loop

1 Like

Based off these replies, try this script instead:

local Humanoid = script.Parent.Parent.Parent.Parent.Humanoid
local HealthText = script.Parent
local Health

Humanoid.HealthChanged:Connect(function()
	Health = Humanoid.Health
	
	HealthText.Text = "Health: ".. math.round(Health)
end)

Here’s what you should avoid doing:

You should avoid calling the value in the first place because the variable would become that value the same forever, while doing this for example:

local Humanoid = script.Parent.Parent.Parent.Parent.Humanoid
local HealthText = script.Parent

while true do
local Health = Humanoid.Health
	wait(1)
	HealthText.Text = "Health: "..Health
end

Calling a value over and over again in a while loop will keep getting the updated value.
Questions?

I was confused on the way you formatted it and didn’t understand much of it, So I just used some of the info from his and made my own that works.

2 Likes

If possible, please share your code in another reply to this topic and mark it as a solution so that others can see what you have done to fix the issue.

2 Likes

Here!

local Humanoid = script.Parent.Parent.Parent.Parent.Humanoid
local Health = Humanoid.Health
local HealthText = script.Parent

local function HealthChanged()
	Health = Humanoid.Health
	HealthText.Text = "Health: "..Health
end

Humanoid.HealthChanged:Connect(HealthChanged)