How do I get a pathfinding NPC to stop at a distance away from the player?

I’m trying to make an AI that shoots the player when they are within a certain distance.

Here is an example using a graph:
{C8955C7E-CE4F-4D78-8762-04C571BB53A4}

X is where the target (player) is and the circle around it is the “deadzone” that the npc must be inside in order to shoot at the player. How can I achieve this? I wasn’t able to find anything about it on the devforum or youtube unless I have missed something.

You would need to use Magnitude for it and constant checking whether something containing a Humanoid is nearby. I would do it this way:

  1. Initial setup
local pathfindingService = game:GetService("PathfindingService")
local spotDistance = 35 --adjust to preference
local stopDistance = 10

while task.wait(0.15) do

end
  1. Is something in workspace that has a humanoid nearby?
local pathfindingService = game:GetService("PathfindingService")
local spotDistance = 35 --adjust to preference
local stopDistance = 10

while task.wait(0.15) do
	for _,v in workspace:GetChildren() do
		if v:FindFirstChildWhichIsA("Humanoid") and v.Name ~= script.Parent.Name and (v.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude <= spotDistance then
			
		end
	end
end
  1. Oh? In that case, let’s make a path to it.
local pathfindingService = game:GetService("PathfindingService")
local spotDistance = 35 --adjust to preference
local stopDistance = 10

while task.wait(0.15) do
	for _,v in workspace:GetChildren() do
		
		if v:FindFirstChildWhichIsA("Humanoid") and v.Name ~= script.Parent.Name and (v.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude <= spotDistance then
			local playerHrp = v.HumanoidRootPart
			local hrp = script.Parent.HumanoidRootPart
			local humanoid = script.Parent.Humanoid
			local distance = (playerHrp.Position - hrp.Position).Magnitude
			
			local path = pathfindingService:CreatePath()
			path:ComputeAsync(hrp.Position,playerHrp.Position)
			
			local waypoints = path:GetWaypoints()
		end
		
	end
end
  1. Right, I need to also make sure to constantly update the distance and actually make it walk to the target. Let’s also ensure to stop whenever it’s approaching too close.
local pathfindingService = game:GetService("PathfindingService")
local spotDistance = 35 --adjust to preference
local stopDistance = 10

while task.wait(0.15) do
	for _,v in workspace:GetChildren() do
		
		if v:FindFirstChildWhichIsA("Humanoid") and v.Name ~= script.Parent.Name and (v.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude <= spotDistance then
			local playerHrp = v.HumanoidRootPart
			local hrp = script.Parent.HumanoidRootPart
			local humanoid = script.Parent.Humanoid
			local distance = (playerHrp.Position - hrp.Position).Magnitude
			
			local path = pathfindingService:CreatePath()
			path:ComputeAsync(hrp.Position,playerHrp.Position)
			
			function updateDistance()
				distance = (playerHrp.Position - hrp.Position).Magnitude
			end
			
			local waypoints = path:GetWaypoints()
			
			for _,wPoint in waypoints do
				humanoid:MoveTo(wPoint.Position)
				if distance < stopDistance then
					hrp.Anchored = true
					repeat
						updateDistance()
						task.wait(0.1)
					until distance > stopDistance
					hrp.Anchored = false
				end
			end
		end
		
	end
end
2 Likes

Is there a way around anchoring the npc’s HRP? Because it keeps stuttering and walking when freezing in place, clashing with my CFrame.lookAt that I have implemented.

As for pathfinding issues, I will deal with them on my own.

You can probably replace it with changing humanoid.WalkSpeed.

that worked, thank you. I’ll set your code that you first sent as the answer to help others.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.