For loop skipping over child of a folder

This script I wrote for a reason I cannot think of is just skipping straight over a module script.

local handlers = script.Parent.Handlers
repeat task.wait() until #handlers:GetChildren() > 0

for i, v in pairs(handlers:GetChildren()) do
	print(v)
	if v:IsA("ModuleScript") then
		print(v)
		local module = require(v)
		module.Init()
	end
end

This script should print all children of the folder, then print only the module scripts
But it just skips over a module script
image
Here is the ancestry of the folder

Here is the output:
image
(I crossed out the irrelevant outputs)

In fact, it skips over two scripts, WagonHandler & DayNightHandler.
I don’t really care that it is skipping over DayNightHandler but the module is vital.

Thanks for any help :heart:

I think that it is because the scripts don’t quite load before the script runs, as the script only waits for 1 script to load, not all. I think a better solution would be just to require them as they’re added to the hierarchy.

local handlers = script.Parent.Handlers
handlers.ChildAdded:Connect(function()
    if v:IsA("ModuleScript") then
		print(v)
		local module = require(v)
		module.Init()
	end
end)

I did think of this, yes, and I forgot to add this in, but while bugfixing this issue myself, I decided to make it print the amount of objects under handlers and it was always 7 (the amount of children normally) and still skipped over them.
Although this script still probably will work.

Edit:
Update, I tried your script and it doesn’t work, this is most likely due to the InitHandlers script being replicated after everything under the Handlers folder.
On Line 1 of the script I threw:

print(handlers:GetChildren)

and get
image
as the output.
I am now more confused as the for loop AFTER this print, does not recognize 2 of the listed children.

Perhaps using the roblox Debugger might be useful here. Insert a breakpoint at the start of the loop and follow your logic throughout its iterations, while checking the hierarchy to make sure that everything is loaded in.

Although I would believe this is an issue of the modules just not loading in on time… ( thought it could be something external code causing this )

I opened the relevant code into a new place file and it works fine so its something else in the game messing with it somehow, thanks for all the help, though :heart:

Edit:
I figured it out, the issue was that the functions in the module scripts had loops that would yield the init script, a really stupid issue that flew under my radar!!! I hope my facepalm-worthy problem helps someone.