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

How do I make a NPC be removed when its health is below 0?

Asked by
Jxemes 75
7 years ago

I'm currently making an FPS/Survival game. I made a Script just for the NPC. To follow the player, to attack when close. I want it so that when the NPC dies, its limbs, torso, and head will be removed after a few seconds. Here's the script.

function onDeath()
    local mobmodel = script.Parent
    if mobmodel.Zombie.Health > 1 then
        wait(3)
        mobmodel:remove()
        else
    end
mobmodel.Zombie.Health.Changed.connect(onDeath)

Am I missing something? Do I need to change something?

1 answer

Log in to vote
1
Answered by
arshad145 392 Moderation Voter
7 years ago
Edited 7 years ago

Hello,

I assume you are new to RbxLua,I will try to help as far as I can.

local d = game:GetService("Debris") -- A service to remove stuff , like a bin.
local mobmodel = script.Parent -- Variables are declared outside of local functions.

mobmodel.Zombie.Humanoid.Died:connect(function()--A Humanoid.Died(function) is better than checking the health everytime it changes.

     --Stuff you want.  
     d:AddItem(mobmodel,2) -- Removes the part after 2 seconds.

end) -- Also do not forget to end loops and functions appropriately.

I am still learning RbxLua.

Thank you for reading.

1
+1, but you should use :Connect instead of :connect as the lowercase one is deprecated. Programical 653 — 7y
Ad

Answer this question