How do I tell when a path has finished?

I’m trying to make a function happen when a robot I made reaches the end a path. Here’s the code.

function calculate(thing)
	local highlight = Instance.new("Highlight")
	highlight.Parent =  thing
	warn(thing.Name.." is the target.")
	local raycastresult = workspace:Raycast(thing.Position, Vector3.new(0,-100,0))
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {thing.Parent}
	if raycastresult then
		calcpath:ComputeAsync(script.Parent.HumanoidRootPart.Position, raycastresult.Position, params)
	end
	--calcpath:ComputeAsync(script.Parent.HumanoidRootPart.Position, workspace.Target.Position)
	script.Parent.Target.Value = thing.Name

	warn("Calculated!")
	if calcpath.Status == Enum.PathStatus.Success then
		local ways = calcpath:GetWaypoints()
		warn("Where to go?")
		for i, point in ipairs(ways) do
			if i == #ways then return end
			warn("I know where to go, can I get there?")
			Humanoid:MoveTo(point.Position)
			print("Go!")
			local pathview = Instance.new("Part")
			pathview.Name = "PathfindTrack"
			pathview.Size = Vector3.new(1,0,1)
			pathview.BrickColor = BrickColor.new("Electric blue")
			pathview.Parent = workspace
			pathview.Position = point.Position
			pathview.Material = Enum.Material.Neon
			pathview.Anchored = true
			pathview.CanCollide = false
			print("run")

			if point.Action == Enum.PathWaypointAction.Jump then
				Humanoid.Jump = true
			end

			Humanoid.MoveToFinished:Wait()
		--	look()
			--script.Parent.HumanoidRootPart.Orientation = CFrame.lookAt(thing.Position, thing.Orientation)
		end
	else
		warn("Uh oh, I couldn't calculate a path!")
		look() -- function for calculating
	end
end

Thanks in advance.

1 Like

You can just add a print at the end of your for loop

for i, point in ipairs(ways) do

What if you just put the function after the for loop?

because the loop is required for the code to work

ive tried this, but when its run on a map, it goes to different parts but never prints
also when it finds a part it never prints it either

1 Like

Normally when the agent reaches the last waypoint, the for loop should end too… So putting the function after the for loop would make it run after the agent has finished traversing the path.

1 Like


here’s footage for when the range for looking is 100
i can upload the entire pathfinding code if you want

1 Like