My npcs both have follow scripts but when they are next to each other they go for each other and even when i get close they still wont attack me can somebody please help
local larm = script.Parent:FindFirstChild("HumanoidRootPart") local rarm = script.Parent:FindFirstChild("HumanoidRootPart") function findNearestTorso(pos) local list = game.Workspace:children() local torso = nil local dist = 20 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,2)) local target = findNearestTorso(script.Parent.HumanoidRootPart.Position) if target ~= nil then script.Parent.Humanoid:MoveTo(target.Position, target) end end
I think a way to fix this is to put a Value or distinct object inside of the npc, then have the script check if they are a npc or not, that way they can be named different things. Example:
A boolvalue that is true called NPC inside of the... NPC. Then in the script:
while true do wait(math.random(1,2)) local target = findNearestTorso(script.Parent.HumanoidRootPart.Position) if target ~= nil then local npc = target:FindFirstChild("NPC", true) --[[The part I added]] if npc == nil then script.Parent.Humanoid:MoveTo(target.Position, target) end end end
Hope this helps!
try to change the name of the humanoids inside of your npcs, and change all the scripts inside of them accordingly