Pathfinding not moving between all waypoints

I am attempting to make a click to move function with pathfinding and although it’s finding a path the player does not follow the path directly and instead skips to the last waypoints position.
Can I get some help I’m a bit of a noob to this?

local agentParams = {
	AgentRadius = 2,
	AgentHeight = 5,
	AgentCanJump = false
}

mouse.Button1Down:Connect(function()
	pos = mouse.Hit.Position
	walkToPoint()
end)

function walkToPoint(x, z)
	local goalX = x
	local goalZ = z
	local goalPosition = Vector3.new(x, 0, z)

	local path = game:GetService("PathfindingService"):CreatePath(agentParams)
	path:ComputeAsync(Char:WaitForChild("HumanoidRootPart").Position, pos)
	local waypoints = path:GetWaypoints()	

	if path.Status == Enum.PathStatus.Success then
		for _, waypoint in pairs(waypoints) do
			local part = Instance.new("Part")
			part.Shape = "Ball"
			part.Material = "Neon"
			part.Size = Vector3.new(0.6, 0.6, 0.6)
			part.Position = waypoint.Position
			part.Anchored = true
			part.CanCollide = false
			part.Parent = game.Workspace
			DebrisService:AddItem(part, 8)
			Humanoid:MoveTo(waypoint.Position)
			wait()
		end
	else
		-- [Pathing failed]
		wait(1)
	end
end

Try implementing MoveToFinished and why is the function after the event shouldn’t it be before?

I tried a few things with MoveToFinished already but couldn’t figure out what to do, also I just so happened to put the function after it wasn’t intentional and did no damage so I didn’t pay attention to it.

edit: also it appears if I raise the wait() to a higher value like wait(1) it will actually follow the path but very slowly because of the wait between each waypoint.

Try replacing the wait() with Humanoid.MoveToFinished:Wait()

1 Like

You’re a god among men, thank you very much!

1 Like

If you don’t mind the other issue I’m trying to work on is clicking a different destination while already moving to one because it causes the player to try and move between the 2 separate paths. I could just debounce it and only allow one path at a time but is there another solution?

You can use break under the loop where you get the waypoints to cancel it, you’d just need to figure out how to execute it once the mouse is pressed again.

1 Like

Yeah not sure how I’ll be able to break it but I’ll try