How do you make a killbrick that kills NPCs only?

I’m not very good at scripting, and I want to know how to make a script for a killbrick that only kills NPCs.

5 Likes

check if the hitted character isnt a player :confused:
use Player.Name == hit.Parent.Name

5 Likes

No problem for that, it’s kind of simple:
Let’s go in order…

  1. Insert a Part

  2. Insert your NPC (my example is ‘NPC’)

  3. Add a BoolValue into the model attribute and call it whatever you like (my example is ‘IsNPC’)

  4. Add a script to the part

  5. Insert the following code:

local NPC = workspace.NPC
script.Parent.Touched:Connect(function()
if NPC:GetAttribute(“IsNPC”) == true then
NPC.Humanoid.Health = -1
end
end)

  1. If everything goes well, only the NPC will get killed by it.

Now, I would recommend using tables to get more NPCs but since your new to scripting I would like you to try this!

3 Likes

Definitely a much better approach then the solution above, even better if we use NPC:HasTag(“NPC”) to even more easily mass distribute these.

3 Likes

if im correct if anything at all including the npc touches this part, the npc gets hurt.

i made a better script which not only fixes that problem but also has a debuf for each npc that touches it.

local npcsDebuf = {}
local debufTime = 2
local healthLost = 1

script.Parent.Touched:Connect(function(part)
	local ModelAncestor = part:FindFirstAncestorWhichIsA('Model')
	
	if npcsDebuf[ModelAncestor] then return end
	
	task.spawn(function()
		npcsDebuf[ModelAncestor] = true
		wait(debufTime)
		npcsDebuf[ModelAncestor] = false
	end)
	
	local ModelHumanoid = ModelAncestor:FindFirstChildWhichIsA('Humanoid')
	
	if not ModelHumanoid then return end
	
	local IsPlayer = game:GetService('Players'):GetPlayerFromCharacter(ModelAncestor)
	
	if IsPlayer then return end
	
	ModelHumanoid['Health'] -= healthLost
end)

i didnt test this code so if theres a problem feel free to tell me

3 Likes