How to cancel pathfinding and stop npc from walking to it!

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? To cancel path find and npc to stop walking.

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub? Most of them made me confused! I couldn’t understand any of them!
    im new to scripting. I tried this so far.
    WalkTo(script.Parent.HumanoidRootPart)

Here is my script so far.

function WalkTo(Position)
		local pathfindingService = game:GetService("PathfindingService")
		local body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("UpperTorso")
		local path = pathfindingService:CreatePath()
		path:ComputeAsync(body.Position, Position.Position)
		local waypoints = path:GetWaypoints()
		for k, waypoint in pairs(waypoints) do
			Humanoid:MoveTo(waypoint.Position)
			Humanoid.MoveToFinished:Wait()
		end
	Humanoid:MoveTo(Position.Position)
	Humanoid.MoveToFinished:Wait()
end
8 Likes

break the loop and set moveto to nil, this will stop it from moving any further

3 Likes

Like this?

			for k, waypoint in pairs(waypoints) do
				if not script.Parent.VALUES.Busy_01.Value  then
					Humanoid:MoveTo(waypoint.Position)
					Humanoid.MoveToFinished:Wait()
				else
					break
				end
				script.Parent.Humanoid:MoveTo(script.Parent.HumanoidRootPart.Position)
			end
3 Likes

seem like it should work, if it dont just change the move to till instead

2 Likes

you could do something more like this

local stop = false

function WalkTo(Position)
		local pathfindingService = game:GetService("PathfindingService")
		local body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("UpperTorso")
		local path = pathfindingService:CreatePath()
		path:ComputeAsync(body.Position, Position.Position)
		local waypoints = path:GetWaypoints()
		for k, waypoint in pairs(waypoints) do
if stop then return end
			Humanoid:MoveTo(waypoint.Position)
			Humanoid.MoveToFinished:Wait()
		end
	Humanoid:MoveTo(Position.Position)
	Humanoid.MoveToFinished:Wait()
end

function Cancel()
stop = true
end

3 Likes

tried script.Parent.Humanoid:MoveTo(nil) it says something like

Argument 1 missing or nil

and the npc still walks until it movestofinished

2 Likes

Hold on I will try this out. Thank you!

2 Likes

i just fixed a redundancy so if you could copy the new code

2 Likes

Thank you! I owe you! Ive been working and struggling on this for 2 days! No joke!

Hold on, is there anyway to resume it?

New Code:

local stop = false

function WalkTo(Position)
stop = false
		local pathfindingService = game:GetService("PathfindingService")
		local body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("UpperTorso")
		local path = pathfindingService:CreatePath()
		path:ComputeAsync(body.Position, Position.Position)
		local waypoints = path:GetWaypoints()
		for k, waypoint in pairs(waypoints) do
if stop then return end
			Humanoid:MoveTo(waypoint.Position)
			Humanoid.MoveToFinished:Wait()
		end
	Humanoid:MoveTo(Position.Position)
	Humanoid.MoveToFinished:Wait()
end

function Cancel()
stop = true
end

You’d want to just run WalkTo again.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.