How do I wait for the path to finish in pathfindingservice

hello I know whats wrong and I know the solution. I just dont know how to do the solution. Bassically I remember there being some sort of path:Wait() or path.completed:Wait() so that it waits for the npc to get to the final path but I forgot its name.

Heres the code:

task.wait(3)
print("going")
local pathfindingService = game:GetService("PathfindingService")

local basePos = workspace.Base.Position
local starting = script.Parent:GetPivot().Position
local direction = (basePos - starting).Unit -- gets the direction vector from NPC to target
local base = game.Workspace.Base
local baseSize = base.Size

-- Calculate a new ending position that is offset from the center of the base
local endingOffset = Vector3.new(0, 0, 0)

if starting.X < base.Position.X then
	endingOffset = Vector3.new(baseSize.X/2 + 10, 0, endingOffset.Z)
end 
if starting.X > base.Position.X then
	endingOffset = Vector3.new(-(baseSize.X/2 + 10), 0, endingOffset.Z)
end     
if starting.Z < base.Position.Z then
	endingOffset = Vector3.new(endingOffset.X, 0, baseSize.Z/2 + 10)
end         
if starting.Z > base.Position.Z then
	endingOffset = Vector3.new(endingOffset.X, 0, -(baseSize.Z/2 + 10))
end

local ending = base.Position + endingOffset * direction

-- Compute the path to the new ending position
local path = pathfindingService:CreatePath()

task.wait(1)

path:ComputeAsync(starting, ending)

for i, waypoint in ipairs(path:GetWaypoints()) do
	local X = math.floor(waypoint.Position.X / 5) * 5
	local Y = math.floor(waypoint.Position.Y / 5) * 5
	local Z = math.floor(waypoint.Position.Z / 5) * 5

	local FinalPos = Vector3.new(X,Y,Z)

	script.Parent.Humanoid:MoveTo(FinalPos)

	print("moving to")
end

PathfindingService and Path APIs have no such signal. PathfindingService is only responsible for generating paths based on agent parameters while the Path object is responsible for giving you the positions created to form the path and occlusion support.

Sounds like you’re thinking about MoveToFinished as far as determining when the Humanoid finishes moving to a certain point. As far as checking when the humanoid moves to the last point in a path, you have to write that part yourself.

1 Like

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