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