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)
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.
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
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)
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)
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)
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)