Problem with non-loaded parts

This might seem like a silly problem

So in the workspace i have a folder with 2 parts
image

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:
image
then there is an error in the output:
image

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:
image
and in fact it prints “Part” 2 times.
image

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 :confused:
image

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.

1 Like

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.

1 Like

If the parts number don’t change over time, then you could use a small loop.

while task.wait(1) do
    if #Creatures:GetChildren() == 2 then
        break
    end
end
2 Likes

make streaming non enabled? make the folder a model and make it atomic so you just wait for the model to load?

2 Likes

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.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.