Pathfinding gets stuck moving between two paths

I’m creating a click to move pathfinding and I’m trying to get it to change direction if you select a new direction while already following a path but instead it creates a 2nd path and tries to move between the waypoints of both.

I’ve tried finding different ways to use break but I can’t seem to get it right and I’d really rather not debounce it so only 1 path can be active.

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
			Humanoid:MoveTo(waypoint.Position)
			Humanoid.MoveToFinished:Wait()
		end
	else
		wait(1)
	end
end

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

Why are you using a pairs table to get 1 point?
Using Humanoid.MoveToFinished:Wait() means that when the point is selected during the MoveTo it always waits until the Humanoid reaches the end before going to the next selected point. Try taking that line out.

1 Like

It gets more than 1 point, it gets every waypoint created using path:GetWaypoints() and the MoveToFinished:Wait() is required in order for the character to actually follow each waypoint instead of skipping straight to the end.

1 Like

You will need a method to cancel the path using break as you have figured out, you can return a function to cancel the path like so:

btw that else wait(1) doesn’t do anything much.


local 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
        local cancelPath = false
        task.spawn(function()
            for _, waypoint in pairs(waypoints) do
                if cancelPath == true then
                    break
                end
                Humanoid:MoveTo(waypoint.Position)
                Humanoid.MoveToFinished:Wait()
            end    
        end)
        local cancelPathFunction = function() 
            cancelPath = true 
        end
        return cancelPathFunction
	end
end

local currentlyHasACancelPathFunction
mouse.Button1Down:Connect(function()
	pos = mouse.Hit.Position
    if (currentlyHasACancelPathFunction) then --if it has this function to cancel the path, that means the humanoid is still walking
        currentlyHasACancelPathFunction()
        currentlyHasACancelPathFunction = nil
    else
        currentlyHasACancelPathFunction = walkToPoint()
    end
end)
3 Likes

Thank you good sir I appreciate it :slightly_smiling_face:

1 Like

It’s been solved already it works now

1 Like