Pathfinding MoveTo() bug

Hi, I’m trying to make a sort of queue, in which I want an NPC to go from point A to point B using the Pathfinding service. Now this is basically my script:

function MoveManager.Create(player)
	local NPC = player.UserNPC.Value
	local MainDirectory = player.Parent
	local Nodes = QueueObjects.Nodes:FindFirstChild(MainDirectory.Name)
	local Position = player.Value
	local TargetPoint = self.GetTarget(Position, Nodes)

	local Path = PathfindingService:CreatePath()

	local Waypoints
	local currentWaypointIndex

	if NPC then
		local Humanoid = NPC:FindFirstChild("Humanoid")

		if Humanoid then
			local Mainpart = NPC:FindFirstChild("HumanoidRootPart")
			
			if Mainpart then
				if TargetPoint then
					
					local function FollowPath(targetPoint)
						print("Starting")
						-- Compute and check the path
						Path:ComputeAsync(Mainpart.Position, targetPoint.Position)
						-- Empty waypoints table after each new path computation
						Waypoints = {}
						
						if Path.Status == Enum.PathStatus.Success then
							-- Get the path waypoints and start zombie walking
							Waypoints = Path:GetWaypoints()
							-- Move to first waypoint
							currentWaypointIndex = 1
							Humanoid:MoveTo(Waypoints[currentWaypointIndex].Position)
						else
							-- Error (path not found); stop humanoid
							Humanoid:MoveTo(Mainpart.Position)
						end
						-- Error (path not found); stop humanoid
					end
					
					local function onWaypointReached(reached)
						print(currentWaypointIndex)
						if reached and currentWaypointIndex < #Waypoints then
							currentWaypointIndex = currentWaypointIndex + 1
							Humanoid:MoveTo(Waypoints[currentWaypointIndex].Position)
						end
					end
					
					local function onPathBlocked(blockedWaypointIndex)
						-- Check if the obstacle is further down the path
						if blockedWaypointIndex > currentWaypointIndex then
							-- Call function to re-compute the path
							FollowPath(TargetPoint)
						end
					end
					
					-- Connect 'Blocked' event to the 'onPathBlocked' function
					Path.Blocked:Connect(onPathBlocked)

					-- Connect 'MoveToFinished' event to the 'onWaypointReached' function
					Humanoid.MoveToFinished:Connect(onWaypointReached)
					
					FollowPath(TargetPoint)
					
				end
			end
		end
	end


end

So basically what this does, is it’s a function called by the rest of my system, which lets the NPC walk to the target point. This is all going well, it works, except for the end. If the NPC is almost at Point B, it starts to randomly glitch a bit, and doesn’t move properly anymore. Also don’t mind the walking animation, doesn’t have anything to do with this script. But this is basically the effect:

https://gyazo.com/377cd97ffce584f34a5ad5f31ea2cf51

The blue circle is point A, and the red circle is point B. Now what I noticed is that when I make the barrier in between the two points shorter, the problem starts at the final dot before point B:

https://gyazo.com/477a32312cf424e80b2f8af2741ffb47

As you can see, it gives this odd sort of “teleporting” effect on the MoveTo(). Now I know this sounds really odd, and honestly, it’s hard to explain and hard to figure out what causes it, but does anyone know what the cause can be of this and how I can prevent this from happening?

Thanks!

2 Likes