Stuck at a npc movement logic

function move(Entity)
	local count = 1
	for i = 1, #game.Workspace.Map.Nodes:GetChildren() do
		if Entity:FindFirstChild("Humanoid") then
			--print(count)
			if count == 7 then
				Entity.HumanoidRootPart.Position = nodes["7"].Position
				task.wait()
				count = 8
				
			else
				print(count)
				Entity.Humanoid:MoveTo(nodes[count].Position)
				Entity.Humanoid.MoveToFinished:Wait()
				count = count + 1
			end
		end
	end
	
	
end

This function moves a npc from node 1 to node 25, and it tps the npc from node 6 to node 7. However after tping the npc from node 6 to node 7, the “count” variable keeps jumping to different numbers. I can’t seem to find out why.
Help will be appreciated !

I don’t get what you meant by jumping to different numbers. Is there a video demonstrating the bug? Also, you should at least wait longer than 1 second after teleporting the humanoid to make sure the count variable is stable.

After the count variable gets to 7, it should be walking the npc node by node as listed above. However it doesn’t and it just keeps teleporting the npc to different nodes, here’s the output that you can see ( the npc is getting teleported to different nodes after node 7 in a second )