How to make Npcs not kill each other?

How to make Zombie Npcs not kill each other?

I have a zombie killing script, but they just kill anything with a humanoidrootpart, including eachother

how do I make it so that they can’t kill eachother?

The Script:

local larm = script.Parent:FindFirstChild("HumanoidRootPartZ")
local rarm = script.Parent:FindFirstChild("HumanoidRootPartZ")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 100
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end




while true do
	wait(math.random(1,5))
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		script.Parent.HumanoidZ:MoveTo(target.Position, target)
	end

end

I think you need to search whether the torso is a player, using Players:GetPlayerFromCharacter()

1 Like

You can do this with a multitude of things, like:

  • Checking if target is not a player (Players:GetPlayerFromCharacter())
  • Checking if target has NPC tag (CollectionService)
  • Checking if target’s name has an NPC name (not recommended)
2 Likes

would this work?

while true do
	wait(math.random(1,5))
	local players = game.Players.PlayerAdded
	local target = players.findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		script.Parent.HumanoidZ:MoveTo(target.Position, target)
	end

end

Try replacing the function with this code:

local Players = game:GetService("Players")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 100
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					if Players:GetPlayerFromCharacter(temp.Parent) then
						torso = temp
						dist = (temp.Position - pos).magnitude
					end
				end
			end
		end
	end
	return torso
end

Sorry for the long wait, when I put I replaced this with the other function, the zombie just stood still. Thank you for your help though. I will keep trying.

1 Like

there are mutiple ways,

  1. you can put all npc’s in another folder
  2. can can also check if a humanoid is the humanoid form a player, using Players:GetPlayerFromCharacter(humanoid.Parent) ~= nil or players:FindFirstChild(Humanoid.Parent.Name) ~= nil
1 Like

now they don’t follow each other, but when they touch, they still kill each other. how do I make it so that they can only kill the player?

1 Like

can i see the script that kills the player?

1 Like
function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
                human.Health = human.Health - 100 -- Change the amount to change the damage.
        end
end
script.Parent.Touched:connect(onTouched)
1 Like

replace it for this:

function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) and game.Players:FindFirstChild(hit.Parent.name) then --the small edit here makes the script only killing players
                human.Health = human.Health - 100 -- Change the amount to change the damage.
        end
end
script.Parent.Touched:connect(onTouched)
5 Likes