I’m working on a tower defense game and I’m trying to make it so that every time a boss spawns, it shows its health on the screen. Works great but for some reason when I kill the boss the screen doesn’t destroy and some health is left (example: 13/1,000). And i’m here because my little brain can’t figure it out.
I’ve tried rewriting the code, but nothing works.
Anyways, how can i fix this? (Sorry for bad English)
workspace.Map.Enemies.ChildAdded:Connect(function(child)
if child and child:GetAttribute("Boss") == true then
local bosshealth = gui.BossHealth.Template:Clone()
bosshealth.Name = child.Name
bosshealth.Parent = gui.BossHealth
bosshealth.Visible = true
bosshealth.BossName.Text = child.Name
while child:GetAttribute("HP") >= 0 do
local hp = child:GetAttribute("HP")
local maxHp = child:GetAttribute("MaxHP")
local shield = child:GetAttribute("Shield")
if hp <= 0 then
bosshealth:Destroy()
break
end
local percent = hp / maxHp
if shield <= 0 then
bosshealth.Health.ShieldDisplay.Visible = false
else
bosshealth.Health.ShieldDisplay.Visible = true
end
bosshealth.Health.Title.Text = format_int(hp + shield) .. "/" .. format_int(maxHp)
bosshealth.BossName.Text = child.Name
bosshealth.Health.CurrentHealth.Size = UDim2.new(math.max(percent, 0), 0, 1, 0)
bosshealth.Health.ShieldDisplay.Size = UDim2.new(shield/maxHp, 0, 1, 0)
task.wait()
end
end
end)
Your conditions are set in such a way that in order for bosshealth:Destroy() to be called HP must both be >= 0 (so the loop runs) and <= 0, so the if statement runs. You should rearrange it so that only the first check is done and the Destroy() is called immediately after the loop exits.