HP is not updating correctly

Hello!

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)

Any help is appreciated,

Thanks.

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.

I made some changes but still have the same issue.

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")
			
			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
		bosshealth:Destroy()
	end
end)

Shouldn’t the condition of the while loop also be:

while child:GetAttribute("HP") > 0 do

Still the same issue, it makes no difference.

Are you removing the boss when its health reaches zero in another script?

1 Like

Yes, i have a script where it spawns, move it and checks:

if enemy:GetAttribute("HP") <= 0 then
	enemy:Destroy()
end

I found the solution! Basically I added cooldown before destroying.

if enemy:GetAttribute("HP") <= 0 then
	task.wait()
	enemy:Destroy()
end

Thank you for your help!

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