SOLVED: Scroll to bottom. Problem: I've been working with this code to make an NPC Pathfind to a set of coordinates that is set by the player's mouse, however, it breaks whenever the coordinates are updated too many times too quickly by the player. This results in the NPC not knowing where to go because(I think) is it trying to navigate to the old positions and the new position since the Pathfind didn't finish/didn't delete the old waypoints.

So far, if a position is set by the player and the NPC moves there and the Pathfind script finishes, THEN new coordinates are set, it works fine. It only breaks when the coordinates are updated mid-travel.

Coordinates are sent to this script from a local script on the player. There is no problem in sending coordinates to server rapidly.

--SERVER SCRIPT--
--service and parameters--
local PathfindingService = game:GetService("PathfindingService")
local pathParams = {
	AgentHeight = 2,
	AgentRadius = 2,
	AgentCanJump = false
}
--remote event--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("remoteEvent")

remoteEvent.OnServerEvent:Connect(function(Player, Position, NPC)
	print(Position, NPC)
	local Humanoid = workspace.NPC.Humanoid
	local Body = workspace.Soldier:FindFirstChild("HumanoidRootPart")

	--compute path--
	local path = PathfindingService:CreatePath(pathParams)
	path:ComputeAsync(Body.Position, Position)

	--waypoints--
	local waypoints = path:GetWaypoints()
	
	--pathfind through waypoints--
	for k, waypoint in pairs(waypoints) do
		wait()
		Humanoid:MoveTo(waypoint.Position)
		Humanoid.MoveToFinished:Wait()
	end
end)

What I think needs to be done is have the script break when the remoteEvent is fired, then run again with the new coordinates (ie. the Pathfind is COMPLETELY cancelled for the old coordinates, waypoints deleted, then script runs again with new coordinates). Problem is I don't know how to do this. Any help is appreciated, thank you in advance.

Solution: After a lot of testing, I noticed with my current scripts, the NPC would correct itself to the new location 100% of the time if the player double clicked. This brought a new problem where I didn't want the player to have to double click. I tried a whole lot of solutions to "simulate" a double click, but I will save you the problem. In your function where you fire your removeEvent, after you fire remoteEvent, write wait() then fire the remoteEvent a second time. This will essentially simulate a double click, and correct the NPC's path 100% of the time from my testing.