How to Kill NPC instead of Player using Touch from killbrick?

(Sorry in advance because I’m new in programming)

So basically I get some kiilbrick from the toolbox that has this script:

--Variables--
local Brick = script.Parent
--End--

--Code--
local function PlayerTouched(Part)
	local Parent = Part.Parent
	if game.Players:GetPlayerFromCharacter(Parent) then
		Parent.Humanoid.Health = 0
	end
end

Brick.Touched:connect(PlayerTouched)

My problem is how to convert those for NPC killing only?

The NPC are group in one as Killers, So I created a for Loop to identify all of the children of killers.

This is what I make:

local Brick = script.Parent


local killers = game.Workspace:WaitForChild("Killers"):GetChildren()


local function EnemyTouched(Part)
	local Parent = Part.Parent

	for i, thisOne in pairs(killers) do
		--As you can see this is blank because I don't know what's next I will do.
        --Also all of the killer's humanoid named as Zombie.
       --So the function of this script should have ..Zombie.Health = 0
	end
	
end

Brick.Touched:Connect(EnemyTouched)
1 Like

You can check if it finds the player in players, if it doesn’t then do the damage.

if not game.Players:FindFirstChild(Part.Parent.Name) then

Sorry, Where do I put this?

30 charssssss

Right before doing any damage to something

local Brick = script.Parent

local function PlayerTouched(Part)
     local Parent = Part.Parent
     -- Validate whether it's some sort of character, player or NPC
     if Parent:FindFirstChild("Humanoid") then
          -- Validate whether it's NOT a player
          if game.Players:GetPlayerFromCharacter(Parent) == nil then
               -- Kill the NPC
               Parent.Humanoid.Health = 0
          end
     end
end
6 Likes