Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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 comment

1

+1, but you should use :Connect instead of :connect as the lowercase one is deprecated.

This discussion is preserved as it was. New answers, comments, and votes are closed.