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.

1function onDeath()
2    local mobmodel = script.Parent
3    if mobmodel.Zombie.Health > 1 then
4        wait(3)
5        mobmodel:remove()
6        else
7    end
8mobmodel.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.

1local d = game:GetService("Debris") -- A service to remove stuff , like a bin.
2local mobmodel = script.Parent -- Variables are declared outside of local functions.
3 
4mobmodel.Zombie.Humanoid.Died:connect(function()--A Humanoid.Died(function) is better than checking the health everytime it changes.
5 
6     --Stuff you want. 
7     d:AddItem(mobmodel,2) -- Removes the part after 2 seconds.
8 
9end) -- 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