So, I’m creating a Healthbar GUI for this NPC in my game, I tried using localscript to make the healthbar works. Yes it works but when the NPC respawns the whole healthbar doesn’t reset completely to full width.
Are there any ways I could possibly make a fully working Boss HP Thingy like GUI?
This is my localscript:
local boss = game.Workspace:WaitForChild("NPC"):WaitForChild("Humanoid")
boss:GetPropertyChangedSignal("Health"):Connect(function()
script.Parent.Size = UDim2.new(boss.Health/ boss.MaxHealth, 0, 1, 0)
end)
This is in-game (Which is right):
The NPC is dead and the Healthbar is 0 value too:
The NPC respawn again but the healthbar didn’t reset:
This is probably due to the fact that when the NPC respawns, the boss variable would need to be redefined so that it knows the new humanoid. The humanoid your variable is currently directed to will be nil once the NPC respawns.
Hey there, when the character dies, it is deleted and readded, thus a new humanoid is instanced into the game. On death, you need to yield and wait for the character to respawn fully, and then reactivate the code you have. So here is how I would do it.
workspace.DescendantAdded:Connect(function(des)
if des.Name == "NPC" then
local boss = des:WaitForChild("Humanoid")
local signal = boss:GetPropertyChangedSignal("Health"):Connect(function()
script.Parent.Size = UDim2.new(boss.Health/ boss.MaxHealth, 0, 1, 0)
end)
local deathsig = boss.Humanoid.Died:Connect(function()
signal:Disconnect()
end)
deathsig:Disconnect()
end
end)
Property changed signal works as intended. Your code would also not work after the NPC died as the boss no longer exists. Plus, you are running that code constantly, which is a waste of resources. By using propertychangedsignal, you are only running the code when it has to run.
Hey, are there any errors in the log? If not, it is likely because the npc was in the workspace on start. Try killing it and seeing if it works the next time around.
Like Official_MrBob101 said, loops aren’t great but try this-
while wait() do
if game.Workspace:WaitForChild(“NPC”) then
local boss = game.Workspace:WaitForChild(“NPC”):WaitForChild(“Humanoid”)
script.Parent.Size = UDim2.new(boss.Health/ boss.MaxHealth, 0, 1, 0)
end
end
-This thing works but, Do I need to kill it first to make the healthbar works?
-Also when it respawn the greenbar doesnt appear, it appears only when the npc takes damage.
No that is ok! I thought this would happen. Here is the updated code, which should work. Good luck
local boss = workspace:WaitForChild("NPC"):WaitForChild("Humanoid") --We need to run the code without a property changed signal for the first time it is in the game
script.Parent.Size = UDim2.new(boss.Health/ boss.MaxHealth, 0, 1, 0)
local signal = boss:GetPropertyChangedSignal("Health"):Connect(function()
script.Parent.Size = UDim2.new(boss.Health/ boss.MaxHealth, 0, 1, 0)
end)
local deathsig = boss.Humanoid.Died:Connect(function()
signal:Disconnect()
end)
deathsig:Disconnect()
boss = nil
workspace.DescendantAdded:Connect(function(des)
if des.Name == "NPC" then
local boss = des:WaitForChild("Humanoid")
script.Parent.Size = UDim2.new(boss.Health/ boss.MaxHealth, 0, 1, 0)
local signal = boss:GetPropertyChangedSignal("Health"):Connect(function()
script.Parent.Size = UDim2.new(boss.Health/ boss.MaxHealth, 0, 1, 0)
end)
local deathsig = boss.Humanoid.Died:Connect(function()
signal:Disconnect()
end)
deathsig:Disconnect()
end
end)
Using that type of loop is incredibly inefficient as I have said multiple times. You should rely on a robust method for handling this sort of stuff. Just because it is easy to do doesn’t mean it is the best way. And just because it works fine once does not mean it will always be fine. :WaitForChild() is only meant to be used when needed, so running it every possible tick in that form of loop is bad practice and slow.
You want to provide the most insightful feedback with scripting support. This person is a learner, so it is best to give them advice that will set them up well. If I were to tell them to do what you are suggesting, they will then use that method in the future. Unbeknownst to them, they won’t realize it is madly inefficient and when making a more complex game, they could suffer from severe performance issues. That is why it is important to not take the quick route when providing a suggestion.