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.