Remove NPC script ain't working

Hello, I’m trying to make a NPC despawn after it dies. I’m a builder but I am trying to learn scripting.

So the script is getting a red line under “= 0 then” here’s the script:

local Zombie = script.Parent

script.Parent.Zombie:Connect(function()
if Zombie.Humanoid.Health = 0 then
wait(1)
Zombie:Destroy()
end

I have tried: Before Zombie:Destroy() it used to be Zombie:Remove()

1 Like

You have to use the == operator.

if Zombie.Humanoid.Health == 0 then

Also, you don’t have an event to connect to the function. You’d probably do Zombie.Humanoid.Changed:Connect(

local Zombie = script.Parent -- this script should be in the zombie model

Zombie.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    -- This function will run whenever the zombie's health changes

    if Zombie.Humanoid.Health <= 0 then
        -- If the zombie's health is 0 or under, then...

        Zombie:Destroy()
        -- ...get rid of the zombie.
    end
end)
2 Likes

Hm it’s still not working. So I’m not supposed to have a function?

No, you should be able to have a function, it’s just that you need an event in order to do anything, .Zombie isn’t an event, it’s an instance or nil (depending on how your hierarchy is set up)

@ScriptingSausage’s example should work.

1 Like