I created a simple localscript in StarterCharacterScripts
local folder = game.Workspace:WaitForChild("Creatures")
for i,v in pairs(folder:GetChildren()) do
print("Part")
end
When i start the game the output doesn’t print anything because the parts haven’t loaded yet. I can confirm this because if i write something like that:
then there is an error in the output:
and if i write something like this there isn’t any error because of the “WaitForChild”
Now if i want the initial loop to work, i have to write a “task.wait()” or a “wait” before the script like this:
and in fact it prints “Part” 2 times.
The problem is that it doesn’t actually work all the time, in fact sometimes it doesn’t even work with the “task.wait()” or the “wait()”. because the parts haven’t loaded yet. I know that i should maybe put “task.wait(1)” or “wait(1)”, but how can i be sure it works all the times? How can i be sure all the parts in the folder have loaded before the loop?
P.S. if i write something like this, it actually works… I’m a bit confused
From my knowledge, it’s impossible to tell when all the parts have loaded inside of a folder. Thankfully, you only have two parts, which means you really don’t need a loop for that, unless I’m missing some information here.
Like what you said in the P.S., just use WaitForChild() when gathering the two parts, and the reason it works is because it yields the current thread until it finds the instance, meaning the parts probably loaded after yielding for the instance.
Ty for your reply. Yeah right now there are only two parts in the folder, but it was just for the example. Later there will be more parts in the folder. So what do you suggest to do? Is it ok if i put “task.wait(1)” at the start of the script?
Yeah, I would recommend it in this scenario. If you want to cover it up, like it is an important part to the game that has to activate instantly when all the parts have loaded, using a short loading screen should do the trick in my opinion, though you can do whatever you want to do to hide it.
You must wait for the children in the folder. You may call this function when another low priority function is ran like when the player joins the game. Mainly, the server loads before the client does.