Hello Robloxians, I am trying to make multiple NPCs follow the closest thing which is a humanoid, but it cannot detect if the thing with a humanoid is an NPC or player, how do I make it so they do not follow each other and can detect if they are following an NPC?
Here is the script:
local NPC = game.Workspace.NPC
local pathfinding_service = game:GetService("PathfindingService")
function getClosestPlayer()
local closest_player, closest_distance = nil, 200
for i, player in pairs(workspace:GetChildren()) do
if player:FindFirstChild("Humanoid") and player ~= NPC then
local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
if distance < closest_distance then
closest_player = player
closest_distance = distance
end
end
end
return closest_player, closest_distance
end
while true do
local path = pathfinding_service:CreatePath()
local player, distance = getClosestPlayer()
if player and distance > 10 then
path:ComputeAsync(NPC.HumanoidRootPart.Position, player.PrimaryPart.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
NPC.Humanoid:MoveTo(waypoint.Position)
while true do
local distance = (NPC.PrimaryPart.Position - waypoint.Position).Magnitude
local distance_from_player = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
if distance < 5 then
break
end
if distance_from_player < 10 then
NPC.Humanoid:MoveTo((NPC.HumanoidRootPart.CFrame*CFrame.new(0,0,-3)).p)
break
end
wait()
end
end
end
wait()
end
You need to check if the character model has a player associated with it. An alternative is:
local playerService = game:GetService("Players")
local playerList = playerService:GetPlayers()
Then inside your loop, use the player list instead of the workspace objects. Doing it this way guarantees that you only get the players, and it’s faster. Using the workspace to get the characters will work, but you will get everything in the game, and it takes longer because it must look at every object in the workspace.
local NPC = game.Workspace.NPC
local pathfinding_service = game:GetService("PathfindingService")
local Players = game:GetService("Players")
function getClosestPlayer()
local closest_player, closest_distance = nil, 200
for i, player in pairs(workspace:GetChildren()) do
if player:FindFirstChild("Humanoid") and Players:GetPlayerFromCharacter(player) then -- Check if character has a player
local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
if distance < closest_distance then
closest_player = player
closest_distance = distance
end
end
end
return closest_player, closest_distance
end
while true do
local path = pathfinding_service:CreatePath()
local player, distance = getClosestPlayer()
if player and distance > 10 then
path:ComputeAsync(NPC.HumanoidRootPart.Position, player.PrimaryPart.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
NPC.Humanoid:MoveTo(waypoint.Position)
while true do
local distance = (NPC.PrimaryPart.Position - waypoint.Position).Magnitude
local distance_from_player = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
if distance < 5 then
break
end
if distance_from_player < 10 then
NPC.Humanoid:MoveTo((NPC.HumanoidRootPart.CFrame*CFrame.new(0,0,-3)).p)
break
end
wait()
end
end
end
wait()
end