How do i make a script that deletes itself after an npc dies?

I have this humanoid npc, and I want the script inside to destroy itself when it dies. how do i do this?

5 Likes

script:Destroy()

You don’t need to destroy the scripts though just let the code go out of scope

The problem is that when the npc dies the script still makes it walk while it’s dead

So your code should specify that the NPC should only walk while it’s alive

How would i do that? I’m not too experienced
And it walks with a while true loop which i’m even less familiar with
I might remove the while true loop though, anyways I need it to go around only once when a button is pressed

How are you making your NPC walk?

1 Like

Get the humanoid, som like this,

local Npc = script.Parent
local Humanoid = Npc:FindFirstChild("Humanoid")

Then just check when it dies

local Npc = script.Parent
local Humanoid = Npc:FindFirstChild("Humanoid")

Humaoid.Died:Connect(Function()
wait(1) -- this waits one secound then deletes the character
Npc:Destroy()
end)

Make sure this script is in the characters model! As a server/sided script

Let me check this, looks promising

1 Like

Actually, it seems like the Humanoid doesnt understand that it died for some reason…
The script doesnt delete itself and everything looks correct

1 Like

Which script are you talking about? also how does it not understand that it died?

I’m not sure, Noob3 is the script i’m using
image

1 Like

Can I see the error that is sent in the command feed back? This code should work

Make sure they die before the script deletes, like humanoid.Died:Connect(function()

1 Like


Only thing that shows up

That is being used in the script above, yet the humanoid is not detecting it died

1 Like

Sorry sorry, I mess up my code i spelled it too fast
Im post the new one

Here is the fix one

local Npc = script.Parent
local Humanoid = Npc:FindFirstChild("Humanoid")

Humanoid.Died:Connect(function()
wait(1) -- this waits one secound then deletes the character
Npc:Destroy()
end)

The last one had Humaiod instead of “Humanoid” on line 4

I think “Function” should be lowercase too but it does the same thing

1 Like

Ya your right is should be lower cased, im not using roblox to make this code so I don’t know if I miss spelled anything, I’ve edited the code above it should be right now

wait, now you have to touch the npc and it falls apart, but the humanoidrootpart keeps going around the track and the script still doesnt delete

2 Likes