My problem
I'm trying to make a NPC destroy itself after being killed by a player, I put a Humanoid
into the dummy, after that I put a server script
into that dummy too, and this is a code inside:
local dummy = script.Parent while true do if dummy.Humanoid.Health == 0 then wait(5) -- destroy after 5 secs dummy:Destroy() end wait(5) -- script cooldown end
The output give me no error
I played the game, kill the dummy, and he still there after 5 secs, i don't know why.
Hope you guys help me.
you need to listen to the died
event of the humanoid..
so assume this script is in the NPC it's self, then the following code should work..
script.Parent.Humanoid.Died:Connect(function() print("NPC has died") script.Parent:Destroy() end)
Improvements
Use WaitForChild() to make sure an instance exists before using it
Use GetPropertyChangedSignal() to check if the health is changed rather than using a loop
You may not be changing the health to exactly 0, so use <= instead of ==
Important Notes
Make sure the Humanoid
's MaxHealth
property and Health
property are set above 0
Revised Server Script
local dummy = script.Parent local human = dummy:WaitForChild("Humanoid") human.GetPropertyChangedSignal("Health"):Connect(function() if human.Health <= 0 then dummy:Destroy() end end)