The code is right here:
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") local plr = Human.Name if players:FindFirstChild(plr) then if (Temp ~= nil) and (Human ~= nil) and (Human.Health > 0) then if (Temp.Position - Position).magnitude < Distance then Torso = Temp Distance = (Temp.Position - Position).magnitude end end end end return Torso end end while true do local target = findNearestPlayer(script.Parent.HumanoidRootPart.Position) if not target.Name == ("Goblin") then --how i try to make them not attack eachother if target ~= nil then script.Parent.Humanoid:MoveTo(target.Position, target) end end end
I do not get any errors.
Only search through players so that you won't have a chance of finding other goblins.
local function findNearestPlayer(pos) local closestRoot local closestDist for _, player in ipairs(game.Players:GetPlayers()) do -- Iterate over only the players local char = player.Character local humanoid = char and char:FindFirstChild("Humanoid") -- The "char and" is just in case a player's character is nil if humanoid and humanoid.Health > 0 then local root = char:FindFirstChild("HumanoidRootPart") if root then local dist = (root.Position - pos).Magnitude if not closestRoot or dist < closestDist then -- if this is the first player we've found or if it's closer than the closest player we've found so far closestRoot = root closestDist = dist end end end end return closestRoot end local root = script.Parent.HumanoidRootPart local humanoid = script.Parent.Humanoid while true do wait(1) -- Don't forget to pause periodically to prevent freezing local target = findNearestPlayer(root.Position) if target then humanoid:MoveTo(target.Position, target) end end
If you want them to attack other NPCs, you'll need to modify the function to look through all models in the workspace that could be a character (just like before), except this time ignore the model if it has a particular name (like "Goblin"), like so:
-- inside the function... for _, model in ipairs(workspace:GetChildren()) do if model.Name == "Goblin" then continue end -- rest of for loop here, using 'model' instead of 'character' end
try:
--[[ function findNearestPlayer(pos) grab things at this position code using region3 and insert them into List code ]] local Torso = 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") local plr = Human.Parent.Name -- changed as Human.Name will return "Humanoid" if players:FindFirstChild(plr) then if (Temp ~= nil) and (Human ~= nil) and (Human.Health > 0) then if (Temp.Position - Position).magnitude < Distance then Torso = Temp Distance = (Temp.Position - Position).magnitude end end end end -- return Torso wrong place this will return anything, might be what's causing your npc's to attack each other end return Torso -- move it to here... end while true do -- we need to change this to account for a nil returning from our findNearestPlayer function local target = findNearestPlayer(script.Parent.HumanoidRootPart.Position) if(target ~= nil) then -- Done! if not target.Name == ("Goblin") then --how i try to make them not attack eachother if target ~= nil then script.Parent.Humanoid:MoveTo(target.Position, target) end end end end
Hope this helps! :)