Pathfinding follow player but keep distances

Ciao,
I’ve been trying to make a pathfinding that follows the player, but it crashes into it.
Is there any way to make it possible for the dummy to stop a few cm before?

Code:

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

local path = PathfindingService:CreatePath()
path:ComputeAsync(Dummy.PrimaryPart.Position, Target)

local waypoints = path:GetWaypoints()
for i = 1,#waypoints do
	humanoid:MoveTo(waypoints[i].Position)
	humanoid.MoveToFinished:Wait()
end

Thank you all!

can you implement this in your script:

local Calculation = HRP.Position - DUMMYHRP.CFrame.LookVector + Vector3.new(5,5)
1 Like

You would calculate distance between player and the NPC as how @zamont124 did.
After you have calculated distance you should check if it’s less than closer distance.

1 Like

Add AgentParameters to PathfindingService:CreatePath

local AgentParameters = {WaypointSpacing = 5}

and then change it such that it checks whether it is the last Waypoint (do not :MoveTo if it is)

local waypoints = path:GetWaypoints()
for i = 1, #waypoints - 1 do
	humanoid:MoveTo(waypoints[i].Position)
	humanoid.MoveToFinished:Wait()
end
1 Like

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