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.
1 | function onDeath() |
2 | local mobmodel = script.Parent |
3 | if mobmodel.Zombie.Health > 1 then |
4 | wait( 3 ) |
5 | mobmodel:remove() |
6 | else |
7 | end |
8 | mobmodel.Zombie.Health.Changed.connect(onDeath) |
Am I missing something? Do I need to change something?
Hello,
I assume you are new to RbxLua,I will try to help as far as I can.
1 | local d = game:GetService( "Debris" ) -- A service to remove stuff , like a bin. |
2 | local mobmodel = script.Parent -- Variables are declared outside of local functions. |
3 |
4 | mobmodel.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 |
9 | end ) -- Also do not forget to end loops and functions appropriately. |
I am still learning RbxLua.
Thank you for reading.