Breaking out of pathfinding to start on a new path? [SOLVED]

I have been working on a top down Sims sorta game. I am currently re-writing my pathfinding. Because previously I patched this but it was more like a band-aid than a solution.

Basically when you right click your character walks along the path to the destination. But if you click again I want it to cancel the old loop and start on the new one. But since its a for loop I am finding it a little difficult.

https://codeshare.io/G6zoYO

That is a link to my code. If you plan to edit it. Please make a copy and paste it below the old code, I want to retain the original code for others.

The problem with my current code is if you click to fast than the 2 for loops dont see that BreakOutOfCurrentPath was equal to true at some point so they continue to run. But if you click and hold the right mouse button it works because the for loops have time to see that they should break.

I know that I am close to solving this. I was going to make it so that you just had to long press to start on a new path but I realized that would get annoying. But I am currently out of ideas as to what to do so I am here asking for your help. Thank you for your time :slight_smile:

4 Likes

Get the loop to check a variable is set to true and exit if it isnt.
And
in the click detection function set the variable to false.

I think thats what I do isnt it?

It would be easier to not use a loop when you navigate a path if you wanted to break and recalculate the current path if blocked or to follow a new path.

I think the script would look something like this:

--[[
local variables up here

function getPoint() --called by navPath
	point = nil
	point is mouse click position
	return point --returns point to navPath
end

function visualPath()
	script here
end

function navPath() --called by mouseclick
	point = getPoint() --calls point at mouseclick position
	
	if point then
		navigate to the point
	end
end

Mouse.Button2Up:Connect(function()
	humanoid:MoveTo(rootPart.Position) --stop moving if already moving
	navPath() --call to calculate path
end)
]]

This is done with pathfinding service. So I dont use mouseclick. Well I do but they navigate to the parts that pathfinding returned.