Cancelling Pathfinding Service and Humanoid:MoveTo()

The title basically explains it all but in simple terms, I want to be able to cancel pathfinding service and stop the character from moving to the next waypoint once I do that. I’m currently making a queue system and just in case a player leaves the queue while in a path to the next waypoint, I don’t want the path to continue after the player has exited the queue.

I’m using the basic pathfinding script that can be found inside the Pathfinding Service Wiki but for reference here is the pathfinding script itself.

local Path
local BreakPath

local function newPath(Destination, Queue, waitForFinish)
	Path = nil
	Path = PathfindingService:CreatePath({
		AgentRadius = 0,
		AgentHeight = 0,
		AgentCanJump = false,
	})
	
	local waypoints
	local nextWaypointIndex
	local reachedConnection
	local blockedConnection
	
	local function followPath(destination)
		local success, errorMessage = pcall(function()
			Path:ComputeAsync(LocalCharacter.PrimaryPart.Position, destination)
		end)
		
		if success and Path.Status == Enum.PathStatus.Success then
			waypoints = Path:GetWaypoints()

			blockedConnection = Path.Blocked:Connect(function(blockedWaypointIndex)
				if blockedWaypointIndex >= nextWaypointIndex then

					blockedConnection:Disconnect()
					followPath(destination)
				end
			end)
			
			if not reachedConnection then
				reachedConnection = LocalCharacter:WaitForChild("Humanoid").MoveToFinished:Connect(function(reached)
					if reached and nextWaypointIndex < #waypoints then
						print("next waypoint")
						nextWaypointIndex += 1
						LocalCharacter:WaitForChild("Humanoid"):MoveTo(waypoints[nextWaypointIndex].Position)
					else
						if waitForFinish then
							NewPath:FireServer()
						end
						
						reachedConnection:Disconnect()
						blockedConnection:Disconnect()
					end
				end)
			end
			nextWaypointIndex = 2
			LocalCharacter:WaitForChild("Humanoid"):MoveTo(waypoints[nextWaypointIndex].Position)
		end
	end
	
	followPath(Destination)
end

I’ve tried things such as returning the function to stop it all together, breaking the function, making the Humanoid:MoveTo(HumanoidRootPart.Position), and setting all values to nil but nothing seems to be stopping the player from continuing their path after exiting the queue.

You can modify the script to include a function that allows the player to cancel the pathfinding and clear the path.
1: Add a new function called cancelPath that will allow the player to cancel the pathfinding.

local function cancelPath()
    
    Path:Cancel()
    
    
    waypoints = {}
    
    
    if blockedConnection then
        blockedConnection:Disconnect()
        blockedConnection = nil
    end
    
    if reachedConnection then
        reachedConnection:Disconnect()
        reachedConnection = nil
    end
end

2:In your original function newPath, you can update the followPath function to include a check for a “cancelled” flag. If the flag is set to true, you can cancel the path and clear the waypoints.

local function followPath(destination)
    local cancelled = false

    
    Path:ComputeAsync(LocalCharacter.PrimaryPart.Position, destination)

    
    if cancelled then
        cancelPath()
        return
    end

   
    if Path.Status == Enum.PathStatus.Success then
        
        waypoints = Path:GetWaypoints()

        -- ...
    else
        -- ...
    end

    -- ...
end

Finally, you can add a button or some other UI element that will set the “cancelled” flag to true when clicked. This will allow the player to cancel the pathfinding and stop the character from moving to the next waypoint.

local cancelButton = script.Parent.CancelButton

cancelButton.Activated:Connect(function()
    
    cancelled = true
end)

Okay, I’ve tried this out but I seem to get the error “Cancel is not a valid member of Path “Instance””. I’m not sure if I’m doing something wrong or if there just isn’t a :Cancel() event on Pathfindingservice.

2 Likes

I know this is a little late, but if I can help, I’d love to.

I am not to fluent with pathfinding, but I do know how to “cancel” a MoveTo() function.

local character = "your character that you want to stop moving"
character:WaitForChild("Humanoid"):MoveTo(character:WaitForChild("HumanoidRootPart").Position)

Hope this helps!

Im sorry i dont have an example available but I’d recommend making the pathfind function get called by using task spawn, and then when you need to cancel it, use task cancel