Not following waypoints when i use MoveToFinished

Im making a npc ai with using pathfinding. Its works good, but there is problem when i delete MoveToFinished

In that video 2023-02-12 19-31-29 npc is following waypoints with MoveToFinished function

but in that video 2023-02-12 19-34-03 im deleting movetofinished function and now its not following waypoints.

Here is the script of AI pathfinding

local soldier = script.Parent
local humanoid = soldier.Humanoid
local pathfindingservice = game:GetService("PathfindingService")

soldier.PrimaryPart:SetNetworkOwner(nil)

local function createPath(destination)
	local pathfindingservice = game:GetService("PathfindingService")
	local path = pathfindingservice:CreatePath()
	path:ComputeAsync(soldier.HumanoidRootPart.Position, destination.HumanoidRootPart.Position)
	for i, w in pairs(workspace.waypoints:GetChildren()) do
		w:Destroy()
	end
	for i, w in pairs(path:GetWaypoints()) do
		local waypoint = Instance.new("Part")
		waypoint.Position = w.Position
		waypoint.CanCollide = false
		waypoint.Parent = workspace.waypoints
		waypoint.Shape = "Ball"
		waypoint.Size = Vector3.new(1, 1, 1)
		waypoint.Anchored = true
	end
	
	return path
end

local function walkTo(destination)
	local path = createPath(destination)
	local waypoints = path:GetWaypoints() 
	for i, w in pairs(waypoints) do
		humanoid:MoveTo(w.Position)
		-- humanoid.MoveToFinished:Wait()
	end
	
end

local function findTarget()
	local targets = workspace.RedTeamSoldiers:GetChildren()
	local maxDistance = math.huge
	local nearestTarget
	if not (targets == nil) then
		for i, t in pairs(targets) do
			local distance = (soldier.HumanoidRootPart.Position - t.HumanoidRootPart.Position).Magnitude
			if distance < maxDistance and t.Humanoid.Health > 0 then
				nearestTarget = t
			end
		end
	else	
		nearestTarget = nil
	end
	return nearestTarget
end


local function patrol()
	local target = findTarget()
	
	if not (target == nil) then
		walkTo(target)
	end
end

while true do
	patrol()
end

What are you doing?

if target then -- Literally the Simplified thing you are trying to do

I don’t understand what you are trying to do with this if it can easily be simplified.

sorry my bad :smile:. Btw that is not problem, the problem is I want to make AI pathfinding but when i delete movetofinished function its not following waypoints, but if i dont delete movetofinished then it moves like in this video: 2023-02-12 19-31-29

look at the difference in the videos, 2023-02-12 19-31-29 and 2023-02-12 19-34-03

in the first one, npc is moving with using moveToFinished function, but its waits and not following good,
But in the second one, i was deleted movetofinished function and now its not following waypoints, why its not following?

sorry for bad english :confused: