How can I stop an NPC's pathfinding when they trigger a prox. prompt

I don’t know how to cancel an NPC’s pathfinding when I use a proximity prompt, I will have multiple NPC’s with different pathfinding and I have a folder named p1 or in other words “Waypoints” named 1-10. If you need any more info I can explain! Thank you for your time! ```lua local Humanoid = script.Parent.Humanoid
local Folder = game.Workspace.Paths.p1 – put folder name here
local loop = true – rename it to false if you don’t want it to loop
local prox = game.Workspace.NPC.Torso.ProximityPrompt

if loop then
while true do
for i, v in pairs(Folder:GetChildren()) do
Humanoid:MoveTo(Folder:FindFirstChild(i).Position)
Humanoid.MoveToFinished:Wait()
end
end
else
for i, v in pairs(Folder:GetChildren()) do
Humanoid:MoveTo(Folder:FindFirstChild(i).Position)
Humanoid.MoveToFinished:Wait()

	prox.Triggered:Connect(function(plr)
		loop = false
		
	end)
end

end ```

1 Like

sorry it glitched out for some reason

You probably want to use coroutines.
I’m looking at your code right now and it’s making little sense to me, so I’m just going to move some things around.

local Humanoid = script.Parent.Humanoid
local Folder = game.Workspace.Paths.p1
local prox = game.Workspace.NPC.Torso.ProximityPrompt

local shouldMove = true

local pathfindCoroutine = coroutine.wrap(function()
	while shouldMove do
		for i, _ in pairs(Folder:GetChildren()) do
			Humanoid:MoveTo(Folder:FindFirstChild(i).Position)
			Humanoid.MoveToFinished:Wait()
		end
	end
end)
coroutine.resume(pathfindCoroutine)

prox.Triggered:Connect(function(_)
	coroutine.yield(pathfindCoroutine)
end)

That should fix your problem. If you get any errors or it doesn’t exactly work, then you can report back here.

The AI didnt work and it doesnt have the loop that I addedc

1 Like

Were there any errors in the Output or did it just do nothing?

lua Workspace.NPC.AI:15: invalid argument #1 to 'resume' (thread expected, got function)

Replace coroutine.wrap with coroutine.create. That was my fault, I haven’t used coroutines in a while.