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)
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)
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.
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)