The "Noobs" in workspace.Noobs folder, won't remove when their health is less than 1

I’m trying to make it so when the noob’s health is less than 1, then they get removed; however, they’re not getting removed, instead just sitting there. I don’t know why.

game.Workspace.Noobs.ChildAdded:Connect(function(child)
	if child.Name:find("Noob") then
		if child.Humanoid.Health <= 1 then
			child:Destroy()
		end
	end
end)

1 Like

The if-statement checks for health status only once when the noob is added. Beyond that, the function does nothing.

I suggest connecting humanoid.Died.

game.Workspace.Noobs.ChildAdded:Connect(function(child)
	if not child.Name:find("Noob") then return end
	-- Not a memory leak because the humanoid gets destroyed
	child.Humanoid.Died:Connect(function()
		child:Destroy()
	end)
end)

It’s still not working for some reason. Is it like just not always detecting or something?

May I ask, how are you changing their health? The changes have to be server-sided. The script also has to be server-sided.

I realised that you are actually destroying the noob once the health reaches 1hp or below, while died fires when the humanoid dies (health reaches 0hp).

In case you’d like to keep the exact functionality you can use HealthChanged.

child.Humanoid.HealthChanged:Connect(function(health)
	if health <= 1 then
		child:Destroy()
	end
end)

Exactly, the if-statement runs a single time. We either need a looped query, or listen to events, which are prefered.

Try this

game.Workspace.Noobs.ChildAdded:Connect(function(child)
	if child.Name:find("Noob") then
		child.Humanoid.Changed:Connect(function(prop) -- check if values change
			if prop == "Health" then -- check if value is health
				if child.Humanoid.Health <= 1 then
					child:Destroy()
				end
			end
		end)
	end
end)

They die when they’re shot, by a gun.

This one worked. I’d like to thank you BOTH for your help.

1 Like

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