How do I pause pathfinding

I am using pathfinding service to move an NPC to a part, but I need to pause the movement if a value is true. For example, if the value is true the NPC will stop then when the value is false it will continue to move on its path. I’ve been looking around for a few days, but I can’t find an answer.

The pathfinding script I’m using (Server)

local RUN = game:GetService("RunService")
local PS = game:GetService("PathfindingService")

local Path = PS:CreatePath({
	
	AgentRadius = 3.5,
	AgentHeight = 5,
	AgentCanJump = false,
	AgentCanClimb = false,
	WaypointSpacing = 4,
	Costs = nil
	
})

task.wait(5)

Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, game.Workspace.test.Position)

for _,  waypoint in pairs(Path:GetWaypoints()) do
	script.Parent.Humanoid:MoveTo(script.Parent.waypoint.Position)
	script.Parent.Humanoid.MoveToFinished:Wait()
end
local RUN = game:GetService("RunService")
local PS = game:GetService("PathfindingService")

local Path = PS:CreatePath({
	
	AgentRadius = 3.5,
	AgentHeight = 5,
	AgentCanJump = false,
	AgentCanClimb = false,
	WaypointSpacing = 4,
	Costs = nil
	
})

task.wait(5)

CantRun = false

Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, game.Workspace.test.Position)

for _,  waypoint in pairs(Path:GetWaypoints()) do
 if CantRun == false then
	script.Parent.Humanoid:MoveTo(script.Parent.waypoint.Position)
	script.Parent.Humanoid.MoveToFinished:Wait()
 end
end

I think that would work. You can just update can run with a value or whatever you want

1 Like

It will stop the NPC from moving, but it won’t move again after. I need to pause it not fully stop it. If that makes sense.

1 Like

Maybe put it in a while loop then add in like a 1 second wait so:

while true do
for _,  waypoint in pairs(Path:GetWaypoints()) do
 if CantRun == false then
	script.Parent.Humanoid:MoveTo(script.Parent.waypoint.Position)
	script.Parent.Humanoid.MoveToFinished:Wait()
 end
task.wait(1)
end

end

It looks like it will work for my purpose, but it has a few weird quirks.

1 Like

I found a better solution. You can set the walk speed to 0 when you need to pause it. Then back to its original speed after.

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