Help With Canceling a pathfinding

Hi. How would I go across canceling a pathfind, in my horror game after a player comes in range with a demon the demon chases them once they die the pathfinding breaks, and the demon tries going to the old destination and walks into walls how would I fix this?

1 Like

This topic’s category should be changed to #help-and-feedback:scripting-support

1 Like

Oh Thanks Ima delete this thanks!

You don’t need to just press the edit button and change the category of it.

1 Like

i did not know thanks!!! :slight_smile:

its okay but changed it to help and feedback…

How are you currently pathfinding? If you want to “cancel” pathfinding then it would be helpful if you provided your current implementation. To cancel pathfinding is to stop the operation from happening midway and I surmise you’re moving through points with a for loop or something.

what do you mean??? like im sorta good at pathfinding but i cant stop the pathfinding when it has not reached its goal yet

What I mean is that it would be helpful if you showed how you’re currently pathfinding. Your code that handles pathfinding for your NPCs. Do you have it? If so, please post it in a code block so we can see exactly how you’re pathfinding right now.

sure it might not be perfect bc i broke it testing stuff but i will send in a min

– get pathfinding service
local pathfindingService = game:GetService(“PathfindingService”)

local num = 1

local JumpScare = game.ReplicatedStorage:WaitForChild(“JumpScare”)

– Variables for NPC humanoid, torso, and destination
local person = script.Parent
local humanoid = script.Parent.Humanoid
local body = script.Parent:WaitForChild(“HumanoidRootPart”) or script.Parent:WaitForChild(“Torso”)

local Locations = game.Workspace.OrangeDemonLocations:GetChildren()

local left = person:FindFirstChild(“LeftEye”)
local right = person:FindFirstChild(“RightEye”)

left.Transparency = 0.75
right.Transparency = 0.75

for _, Part in pairs(person:GetDescendants())do
if Part:IsA(“BasePart”) then
if Part:CanSetNetworkOwnership() then
Part:SetNetworkOwner();
end;
end;
end;

local function findTarget()
local players = game.Players:GetChildren()
local maxDistance = 50
local nearestTarget = nil
local target = nil

for i, v in pairs(players) do
	repeat wait()
		
	until v.Character
	
	local target = v.Character
	local distance = (body.Position - target.HumanoidRootPart.Position).Magnitude

	if distance < maxDistance then
		nearestTarget = target
		maxDistance = distance
	end
		
end
return nearestTarget

end

local value = false

local function attack(Char)

num = 0

left.Transparency = 0
right.Transparency = 0




person.Humanoid.WalkSpeed = 8

repeat wait(0.2) 
	
	local newpath = pathfindingService:CreatePath()
	
	newpath:ComputeAsync(body.Position, Char.HumanoidRootPart.Position)

	-- get the waypoints table
	

	local Points = newpath:GetWaypoints()

-- iterate through all waypoints, and jump when necessary
	for k, Waypoint in pairs(Points) do
		humanoid:MoveTo(Waypoint.Position)

		humanoid.MoveToFinished:Wait()
	end
	
	local distance = (body.Position - Char.HumanoidRootPart.Position).Magnitude
	
	local maxDistance = 7
	
	
	
	local cancleDistance = 40
	
	if maxDistance > distance then
		JumpScare:Fire(Char)
		--num = 1
		value = true
	end
	
	if distance > cancleDistance then
		print("away")
		--num = 1
		value = true
	end
	
	
	
until value == true 

local newpath = pathfindingService:CreatePath()

-- compute a path

local randomNum = math.random(1, #Locations)

newpath:ComputeAsync(body.Position, Locations[randomNum].Position)

-- get the waypoints table

local Waypoints = newpath:GetWaypoints()

for k, Waypoint in pairs(Waypoints) do
	humanoid:MoveTo(Waypoint.Position)

	humanoid.MoveToFinished:Wait()
	
	
end


num = 1
-- make it so it goes back AND does not hit walls

--local path = pathfindingService:CreatePath()

--local randomNum = math.random(1, #Locations)

--path:ComputeAsync(body.Position, Locations[randomNum].Position)

	-- get the waypoints table


--local Points = path:GetWaypoints()

	-- iterate through all waypoints, and jump when necessary
--for k, Waypoint in pairs(Points) do
--	humanoid:MoveTo(Waypoint.Position)

–
– humanoid.MoveToFinished:Wait()
– end
end

local function patrol()

left.Transparency = 0.75
right.Transparency = 0.75

local randomNum = math.random(1, #Locations)

local newpath = pathfindingService:CreatePath()

-- compute a path
newpath:ComputeAsync(body.Position, Locations[randomNum].Position)

-- get the waypoints table

local Waypoints = newpath:GetWaypoints()

--for k, Waypoint in pairs(Waypoints) do
--	local h = Instance.new("Part")
--	h.Parent = game.Workspace
--	h.Material = "Neon"
--	h.Size = Vector3.new(0.25,0.25,0.25) 
--	h.Position = Waypoint.Position
--	h.Anchored = true
--end


local Track = Instance.new("Animation")
Track.AnimationId = "rbxassetid://6047321618"
local Anim = person.Humanoid:LoadAnimation(Track)
Anim:Play()

-- iterate through all waypoints, and jump when necessary
for k, Waypoint in pairs(Waypoints) do
	humanoid:MoveTo(Waypoint.Position)
	
	humanoid.MoveToFinished:Wait()
	
	local target = findTarget()

	if target then
		attack(target)
		num = 1
	end
end
	
--end

num = 1
--humanoid.MoveToFinished:Wait()

end

while wait(0.25) do
person.Humanoid.WalkSpeed = 6

if num == 1 then
	
	num = 2
	
	local target = findTarget()
		
	if target then
		attack(target)
	elseif target == nil then
		
	end
	
	patrol()

end

end

its a bit sloppy but here it is

It seems like you have everything down for the most part already. If you need to completely cancel the pathfinding, seems logical to just throw a break statement at the bottom of your if statements which are checked after using findTarget. break terminates a loop, which would stop your NPC from continuing to follow nodes.

thanks! i will try this :slight_smile:

wait im confused exactly where???

1 Like

I know it has been a long time scine this post .I would like to know if it worked