I have two NPC's down on the level and they attack eachother as soon as I run it.
local larm = script.Parent:FindFirstChild("Left Arm") local rarm = script.Parent:FindFirstChild("Right Arm") 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("Torso") 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(0.1) local target = findNearestTorso(script.Parent.Torso.Position) if target ~= nil then script.Parent.Humanoid:MoveTo(target.Position, target) end end
I have found the problem, in the scripting I said "findnearesttorso" but I don't know any other way of doing this.
Is there any way I can keep NPC's from attacking eachother but attack players?
Okay. I modified the script. All you have to do is change the name of the Humanoid to NPC (Including caps). This will probably break your other scripts. But they can be fixed by changing one word.
local larm = script.Parent:FindFirstChild("Left Arm") local rarm = script.Parent:FindFirstChild("Right Arm") 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("Torso") 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(0.1) local target = findNearestTorso(script.Parent.Torso.Position) if target ~= nil then script.Parent.NPC:MoveTo(target.Position, target) end end
Not entirely sure of what the NPCs do to the player or each other. I need some more information like: Do the NPCs hurt when touching each other? Do NPCs battle other NPCs? Etc. Anyways, changing the humanoid is recommended, but if you have a variety of NPCs which it seems that you do, have another if statement at line 13. For example, within the NPC you can change something that distinguishes it like a string value, or a body color, or a script name.
Ex): if (temp2.className == "Model") and (temp2 ~= script.Parent) and FindFirstChild:("Scriptname/BodyColor/StringValue").Value/Name/BrickColor == "Name/Value/Color" then