How to make pathfinding not target certain players

I have a pathfinding script, but I want it to not target people who are on a certain team, how do I make that?

This is the script I have currently

local npc = script.Parent
local human = npc.Humanoid

local PFS = game:GetService("PathfindingService")
local RUNSERVICE = game:GetService("RunService")

npc.PrimaryPart:SetNetworkOwner(nil)

local function findTarget()
	local players = game:GetService("Players"):GetPlayers()
	local nearesttarget
	local maxDistance = 75 -- distance

	for i,player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (npc.HumanoidRootPart.Position - target:WaitForChild("HumanoidRootPart").Position).Magnitude

			if distance < maxDistance then
				nearesttarget = target
				maxDistance = distance
			end

		end
	end
	return nearesttarget
end

local function getPath(destination)
	local path = PFS:CreatePath()

	path:ComputeAsync(npc.HumanoidRootPart.Position, destination)

	return path
end

local function pathFindTo(destination)
	local path = getPath(destination)
	local target = findTarget()

	if target and target.Humanoid.Health > 0 then
		for i,waypoint in pairs(path:GetWaypoints()) do

			if waypoint.Action == Enum.PathWaypointAction.Jump then
				human.Jump =true
			end

			human:MoveTo(waypoint.Position)
			human.MoveToFinished:Wait()

		end
	end
end

RUNSERVICE.Heartbeat:Connect(function()
	local target = findTarget()

	if target then
		pathFindTo(target:WaitForChild("HumanoidRootPart").Position + (target:WaitForChild("HumanoidRootPart").Velocity.Unit * 1))
	end
end)
2 Likes

In this part of your script simply add an if statement that checks if the player’s team is equal to the one you want to target.

1 Like

This works, but even if I’m not on that team anymore, it won’t target me
How do I fix that?

Create a boolean value under the player called “CanChase”. For players you don’t want to be chased, set it to false and in your pathfinding logic add an if statement to check if the bool is true.

this gives me the same issue as before

what do you mean by that? The team issue? I am not suggesting you check team, i am suggesting adding a separate bool to signify whether the npc can chase the player.

The same Issue that I am talking about is even if that boolValue is true, if it was false before, it will still chase me, and if it was true before and i make it false, it wont chase me

I don’t think so… are you not updating the path/target on each heartbeat?

Man I don’t even know, I’m awful at scripting.

Okay here let me show you what I mean. In the following code, if you want the npc to be able to chase the player do SetPlayerChase(Player, true)
and if you don’t then do
SetPlayerChase(Player, false)
all players will be chaseable by default.

local playerService = game:GetService("Players")
--local players=playerService:GetPlayers()


playerService.PlayerAdded:Connect(function(player) --give chase bool to all new players
	local bool = Instance.new("BoolValue")
	bool.Parent = player
	bool.Name = "CanChasePlayer"
	bool.Value = true
end)

local function SetPlayerChase(player, boolValue)
	player:WaitForChild("CanChasePlayer").Value = false
end



local npc = script.Parent
local human = npc.Humanoid

local PFS = game:GetService("PathfindingService")
local RUNSERVICE = game:GetService("RunService")

npc.PrimaryPart:SetNetworkOwner(nil)


local function findTarget()
	local players = game:GetService("Players"):GetPlayers()
	local nearesttarget
	local maxDistance = 75 -- distance

	for i,player in pairs(players) do
		if player:FindFirstChild("CanChasePlayer").Value == true then
			print("hi")
			if player.Character then
				local target = player.Character
				local distance = (npc.HumanoidRootPart.Position - target:WaitForChild("HumanoidRootPart").Position).Magnitude

				if distance < maxDistance then
					nearesttarget = target
					maxDistance = distance
				end

			end 
		end
	end
	return nearesttarget
end

local function getPath(destination)
	local path = PFS:CreatePath()

	path:ComputeAsync(npc.HumanoidRootPart.Position, destination)

	return path
end

local function pathFindTo(destination)
	local path = getPath(destination)
	local target = findTarget()

	if target and target.Humanoid.Health > 0 then
		for i,waypoint in pairs(path:GetWaypoints()) do

			if waypoint.Action == Enum.PathWaypointAction.Jump then
				human.Jump =true
			end

			human:MoveTo(waypoint.Position)
			human.MoveToFinished:Wait()

		end
	end
end

RUNSERVICE.Heartbeat:Connect(function()
	local target = findTarget()

	if target then
		pathFindTo(target:WaitForChild("HumanoidRootPart").Position + (target:WaitForChild("HumanoidRootPart").Velocity.Unit * 1))
	end
end)

This doesn’t even work at all

local playerService = game:GetService("Players")
local players=playerService:GetPlayers()
for _, player in players do -- give chase bool to all current players
	local bool = Instance.new("BoolValue", player)
	bool.Name = "CanChase"
	bool.Value = true
end

playerService.PlayerAdded:Connect(function(player) --give chase bool to all new players
	local bool = Instance.new("BoolValue", player)
	bool.Name = "CanChase"
	bool.Value = true
end)

local function SetPlayerChase(player, boolValue)
	player:WaitForChild("CanChase").Value = false
end



local npc = script.Parent
local human = npc.Humanoid

local PFS = game:GetService("PathfindingService")
local RUNSERVICE = game:GetService("RunService")

npc.PrimaryPart:SetNetworkOwner(nil)

local function findTarget()
	local players = game:GetService("Players"):GetPlayers()
	local nearesttarget
	local maxDistance = 75 -- distance

	for i,player in pairs(players) do
		if player:FindFirstChild("Bool").Value == true then
			if player.Character then
				local target = player.Character
				local distance = (npc.HumanoidRootPart.Position - target:WaitForChild("HumanoidRootPart").Position).Magnitude

				if distance < maxDistance then
					nearesttarget = target
					maxDistance = distance
				end

			end 
		end
	end
	return nearesttarget
end

local function getPath(destination)
	local path = PFS:CreatePath()

	path:ComputeAsync(npc.HumanoidRootPart.Position, destination)

	return path
end

local function pathFindTo(destination)
	local path = getPath(destination)
	local target = findTarget()

	if target and target.Humanoid.Health > 0 then
		for i,waypoint in pairs(path:GetWaypoints()) do

			if waypoint.Action == Enum.PathWaypointAction.Jump then
				human.Jump =true
			end

			human:MoveTo(waypoint.Position)
			human.MoveToFinished:Wait()

		end
	end
end

RUNSERVICE.Heartbeat:Connect(function()
	local target = findTarget()

	if target then
		pathFindTo(target:WaitForChild("HumanoidRootPart").Position + (target:WaitForChild("HumanoidRootPart").Velocity.Unit * 1))
	end
end)


Can you show your code that tries to check the player’s team? It’s hard to help you fix the bug when you haven’t provided updated code.

local npc = script.Parent
local human = npc.Humanoid

local PFS = game:GetService("PathfindingService")
local RUNSERVICE = game:GetService("RunService")

npc.PrimaryPart:SetNetworkOwner(nil)

local function findTarget()
	local players = game:GetService("Players"):GetPlayers()
	local nearesttarget
	local maxDistance = 250 -- distance
	
	for i,player in pairs(players) do
		if player.TeamColor ~= BrickColor.new("Really black") then
		if player.Character then
			local target = player.Character
			local distance = (npc.HumanoidRootPart.Position - target:WaitForChild("HumanoidRootPart").Position).Magnitude

			if distance < maxDistance then
				nearesttarget = target
				maxDistance = distance
			end
		end
		end
	end
	return nearesttarget
end

local function getPath(destination)
	local path = PFS:CreatePath()

	path:ComputeAsync(npc.HumanoidRootPart.Position, destination)

	return path
end

local function pathFindTo(destination)
	local path = getPath(destination)
	local target = findTarget()

	if target and target.Humanoid.Health > 0 then
		for i,waypoint in pairs(path:GetWaypoints()) do

			if waypoint.Action == Enum.PathWaypointAction.Jump then
				human.Jump =true
			end

			human:MoveTo(waypoint.Position)
			human.MoveToFinished:Wait()

		end
	end
end

RUNSERVICE.Heartbeat:Connect(function()
	local target = findTarget()

	if target then
		pathFindTo(target:WaitForChild("HumanoidRootPart").Position + (target:WaitForChild("HumanoidRootPart").Velocity.Unit * 7))
	end
end)

Have you double checked that the team color is correct?

whoops i made a few mistakes. Here is the fixed script:

local playerService = game:GetService("Players")
--local players=playerService:GetPlayers()


playerService.PlayerAdded:Connect(function(player) --give chase bool to all new players
	local bool = Instance.new("BoolValue")
	bool.Parent = player
	bool.Name = "CanChasePlayer"
	bool.Value = true
end)

local function SetPlayerChase(player, boolValue)
	player:WaitForChild("CanChasePlayer").Value = boolValue
end



local npc = script.Parent
local human = npc.Humanoid

local PFS = game:GetService("PathfindingService")
local RUNSERVICE = game:GetService("RunService")

npc.PrimaryPart:SetNetworkOwner(nil)


local function findTarget()
	local players = game:GetService("Players"):GetPlayers()
	local nearesttarget
	local maxDistance = 75 -- distance

	for i,player in pairs(players) do
		if player:FindFirstChild("CanChasePlayer").Value == true then
			print("hi")
			if player.Character then
				local target = player.Character
				local distance = (npc.HumanoidRootPart.Position - target:WaitForChild("HumanoidRootPart").Position).Magnitude

				if distance < maxDistance then
					nearesttarget = target
					maxDistance = distance
				end

			end 
		end
	end
	return nearesttarget
end

local function getPath(destination)
	local path = PFS:CreatePath()

	path:ComputeAsync(npc.HumanoidRootPart.Position, destination)

	return path
end

local function pathFindTo(destination)
	local path = getPath(destination)
	local target = findTarget()

	if target and target.Humanoid.Health > 0 then
		for i,waypoint in pairs(path:GetWaypoints()) do

			if waypoint.Action == Enum.PathWaypointAction.Jump then
				human.Jump =true
			end

			human:MoveTo(waypoint.Position)
			human.MoveToFinished:Wait()

		end
	end
end

RUNSERVICE.Heartbeat:Connect(function()
	local target = findTarget()

	if target then
		pathFindTo(target:WaitForChild("HumanoidRootPart").Position + (target:WaitForChild("HumanoidRootPart").Velocity.Unit * 1))
	end
end)

Yes

ignore this part I have to put this here for some reason

Try printing player.TeamColor after your check to see what it says.

I already told you, the teamColor is fine

I fixed your script please try it.

Still the same issue

ignore this