Stop when it doesnt finds the next part on for i = .. on a folder

Whats up! so, im a pretty advanced scripter and im making a tower defense game, but im running into a problem that is, the system that i use for making the enemy move to the next nodes is for i = 1, #nextnodes do, and i was wondering if there was a way to do something like this:

If the loop doesnt finds another child to progress then it stops, can anyone help me?

The script i have tried


local storage = game:GetService("ReplicatedStorage")
local tweenservice = game:GetService("TweenService")
local zombie = storage.VeryBadGuy

local nodes = workspace.Nodes

function spawnmob()
	local mob = zombie:Clone()
	mob.HumanoidRootPart.CFrame = workspace.Start.CFrame
	mob.Parent = game.Workspace
	
	moveenemy(mob)
end

function moveenemy(enemy)
	local zombie = enemy
	local nextnodes = nodes:GetChildren()
	local finished = false
	
	for i = 1, #nextnodes do
		if finished == false then
			if not i then
				finished = true
				task.wait(0.1)
				
				enemy:Destroy()
				break
			end
			local move = tweenservice:Create(enemy.HumanoidRootPart, TweenInfo.new(2.75, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {Position = nextnodes[i].Position})
			move:Play()
			move.Completed:Wait()
		end
	end
end

while true do
	task.wait(2.75)
	coroutine.wrap(spawnmob)()
end

(if not i then is the part that will run if it doesnt finds the next child)

why not use for i,v in ipairs(nodes:GetChildren()) do?

it will run until there is not i left

i mean, i tried to use it and the enemies just moved o random nodes bc they are not in order, but then i would run into the same issue of when to check if it doesnt finds a new child tho

And im also pretty sure that it would be the same thing

Wait nevermind, i just needed to add it after the for i = loop, but thx!

if not i then

i will always be true, perhaps you meant if i == nextnodes then.