Healthbar does not update to the previous health

I made a healthbar for a zombie titan where the healthbar is in the form of a screengui.Hbar

but for some reason, the blood doesn’t update when the server :destroy zombies
(for example player damage zombie 50/100, then server :destroy all zombies because time runs out)

when a new round starts, the health of the screengui.Hbar starts from 100/100 again instead of 50/100

the stage of how its happening:

  • server clone the zombie to wspace
    image
local zmb = ss[random]:Clone()
	ss.Parent = workspace

.

  • hbar script, will retrieve health data from workspace.Zombie.Humanoid.Health
    image
local Character = game.Workspace:WaitForChild("R6")
local hum = Character:WaitForChild("Humanoid") -- player humanoid
HpText.Text = string.format("Health: %s/%s", hum.Health, hum.MaxHealth) -- The hp TextLabel
etc...

so how do I get ,health updates to previous health?

You can perhaps try using the GetPropertyChangedSignal function for this. And then just put your code again

how to do it, sorry I’m new to coding.

1 Like
local Character = game.Workspace:WaitForChild("R6")
local hum = Character:WaitForChild("Humanoid") -- player humanoid
HpText.Text = string.format("Health: %s/%s", hum.Health, hum.MaxHealth) -- The hp TextLabel

I don’t think you’re changing the health every time, that might be causing it, try using a while loop, like this:

local Character = game.Workspace:WaitForChild("R6")
local hum = Character:WaitForChild("Humanoid") -- player humanoid
while task.wait(1) do
     HpText.Text = string.format("Health: %s/%s", hum.Health, hum.MaxHealth) -- The hp TextLabel
end

You wanna run that code everytime the health changes, do this with ‘PropertyChangedSignal()’.

local Character = game.Workspace:WaitForChild("R6")
local hum = Character:WaitForChild("Humanoid") -- player humanoid
function UpdateGui()
   HpText.Text = string. Format("Health: %s/%s", hum.Health, hum.MaxHealth) -- The hp TextLabel
end
hum:GetPropertyChangedSignal('Health'):Connect(UpdateGui)
1 Like

It should be something like this(im not on my pc so this may not work)

Humanoid.Health.GetPropertyChangedSignal(“Health”):Connect(function()
–your code here
end)

1 Like

I guess the “health” needs to be “Health” instead, but i get it, it’s hard to type correctly in mobile

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.