How can I cancel the Old Path?

Hi. I am using PathFindingService, and I want to control NPC by clicking.
When I first click, it works normally, but when I click the second, it will first go to the second path waypoints and go back to the first path waypoints, and go to the second path waypoints again, etc.

--script on starterplayerscript
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local RS = game.ReplicatedStorage
local event = RS.pos


mouse.Button1Up:Connect(function()
	event:InvokeServer(mouse.Hit.Position)
end)
-- Script that put on the NPC
local pathfinding = game:GetService("PathfindingService")
local human = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("Torso")
local RS = game.ReplicatedStorage
local event = RS.pos
local position = Vector3.new(0,0,0)
local path = pathfinding:CreatePath()
local pos = Vector3.new(0,0,0)
local waypointss = {}


event.OnServerInvoke = function(player, pos)
waypointss = {}
if 	game.Workspace:FindFirstChild("pathmodel") then
game.Workspace:FindFirstChild("pathmodel"):Destroy() end
local pathmodel = Instance.new("Model", game.Workspace)
pathmodel.Name = "pathmodel"

path:ComputeAsync(torso.Position, torso.Position)
human:MoveTo(torso.Position)
	

path:ComputeAsync(torso.Position, pos)
waypointss = path:GetWaypoints()
for i, waypoint in pairs (waypointss) do
	local point = Instance.new("Part")
	point.Shape = "Ball"
	point.Parent = pathmodel
	point.Position = waypoint.Position - Vector3.new(0, 0.3, 0)
	point.Material = "Neon"
	point.Size = Vector3.new(1,1,1)
	point.Anchored = true
	point.CanCollide = false
	point.Transparency = 0.3
end


for i, waypoint in pairs (waypointss) do
	human:MoveTo(waypoint.Position)
	human.MoveToFinished:Wait(2)
end

script.Parent:WaitForChild("Humanoid"):MoveTo(pos)
end

I had cleared the table of 1st path waypoints before it ran the second path, but it still runs.

How to cancel the old path??

1 Like
-- Script that put on the NPC
local pathfinding = game:GetService("PathfindingService")
local human = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("Torso")
local RS = game.ReplicatedStorage
local event = RS.pos
local position = Vector3.new(0,0,0)
local path = pathfinding:CreatePath()
local pos = Vector3.new(0,0,0)
local waypointss = {}
local debounce = false

event.OnServerInvoke = function(player, pos)
	if debounce == false then
		debounce = true
		path:ComputeAsync(torso.Position, pos)
		waypointss = path:GetWaypoints()

		for i, waypoint in pairs (waypointss) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				human:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			human:MoveTo(waypoint.Position)
			human.MoveToFinished:Wait(2)
		end
		debounce = false
	end
end

could do this but the script waits for the first path to finish then it proceeds to the next one, I’ll try to figure out how to cancel a path

1 Like

sorry but if you add
if waypoint.Action == Enum.PathWaypointAction.Jump then
human:ChangeState(Enum.HumanoidStateType.Jumping)
end
then it will be a little laggy, sorry im not the best with pathfinding

1 Like

I’m sorry but I can’t wait for the first path to finish then proceeds to the next one, or else the game will be so weird :confused:

1 Like

It’s fine and ill try to add it in my game but how to cancel the old path, that’s the one I confused most!

1 Like

You need to break the for loop when the player clicks again because the first for loop is still running when you click again and you start another one when you click again which is your problem

1 Like

May I ask how to break the loop?

1 Like

You just put a link the loop called break, so what you need to do if the player clicks again then break the loop then have it run again

1 Like

Thanks! But how can I have it run again?

1 Like