I scripted it so that the Bot stops 6 studs away from the Pathfinding bot but it walks/looks in the other direction .
The problem should be at the end of the script.
— Server Script
local Humanoid = script.Parent.Humanoid
local SpawnPoint = script.Parent.SpawnPosition
local Animator = Humanoid:WaitForChild("Animator")
local NpcHrp = script.Parent.HumanoidRootPart
local PFS = game:GetService("PathfindingService")
local path = PFS:CreatePath()
local Distance
local Nearestplayer
local PlayerDistance
local Waypoints
while true do -- NPC Cycle
local ShortestDistance = math.huge
repeat -- checks every Player and their distance until one is <= 30 away
for i, player in game.Players:GetPlayers() do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then -- check if Player is ingame
Distance = (SpawnPoint.Position - player.Character.HumanoidRootPart.Position).Magnitude
if Distance < ShortestDistance then
ShortestDistance = Distance
NearestPlayer = player
end
end
end
wait()
until ShortestDistance <= 30
repeat
spawn(function() -- follow player
repeat
for i, player in game.Players:GetPlayers() do
if NearestPlayer.Character and NearestPlayer.Character:FindFirstChild("HumanoidRootPart") then -- check if Player is ingame
Distance = (SpawnPoint.Position - NearestPlayer.Character.HumanoidRootPart.Position).Magnitude
PlayerDistance = (NpcHrp.Position - NearestPlayer.Character.HumanoidRootPart.Position).Magnitude
print(Distance)
print(PlayerDistance)
if Distance < ShortestDistance then
ShortestDistance = Distance
end
end
end
wait()
until true
end)
if Distance <= 30 and NearestPlayer.Character.Humanoid.Health ~= 0 then
while PlayerDistance == nil do wait() end
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, NearestPlayer.Character.HumanoidRootPart.Position)
Waypoints = path:GetWaypoints()
for i, waypoint in pairs(Waypoints) do
if Distance >= 30 and NearestPlayer.Character.Humanoid.Health == 0 then break end
if PlayerDistance <= 6 then end
if (waypoint.Position - NearestPlayer.Character.HumanoidRootPart.Position).Magnitude <= 6 then break end
if waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
Humanoid:MoveTo(waypoint.Position)
end
end
until Distance >= 30 or NearestPlayer.Character.Humanoid.Health == 0
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, SpawnPoint.Position) -- Go back to Spawn
local Waypoints = path:GetWaypoints()
for i, waypoint in pairs(Waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
Humanoid:MoveTo(waypoint.Position)
end
end