Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a NPC destroy itself after being killed?

Asked by
c0nc3rt 61
5 years ago

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

2 answers

Log in to vote
1
Answered by 5 years ago

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)
Ad
Log in to vote
0
Answered by 5 years ago

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)
0
why not use humanoid's died event?. royaltoe 5144 — 5y
0
If you disabled the HumanoidStateType.Dead then Humanoid.Died would not run SerpentineKing 3885 — 5y

Answer this question