Waiting for parts in a folder to load

I am trying to do some things with parts, however, it appears that the parts are not loaded yet at the moment, so in a for loop like

for key, value in color:GetChildren() do

the loop never runs because color:GetChildren is empty.

I have tried to use content provider to load the parts

local assets = {blue["plate"], blue["Floor left"], blue["Floor right"], red["plate"], red["Floor left"], red["Floor right"]}
ContentProvider:PreloadAsync(assets, nil)

however, that results in an error “plate is not a valid member of Folder “Workspace.Blue” - Client - LocalScript:32” I am also planning to add more items into the folder, so I don’t want to WaitForChild all of the parts I need. Is there a solution to wait for all the parts to load inside of a folder

Use a repeat task.wait() loop until game:IsLoaded().

1 Like

I have tried that but it didn’t seem to have worked

1 Like

Make sure the loop comes before the code you are trying to execute that indexes the colors. If it still doesn’t work, you could try brute forcing it and just putting a wait function for a set amount of time so the game has time to load.

so this is the code with the loop

local ContentProvider = game:GetService("ContentProvider")

local status = 0
-- 0 = blue, 1 = middle, 2 = red
local blue = workspace:WaitForChild("Blue")
local red = workspace:WaitForChild("Red")


function setMiddle()
	
end

function changeColors(newStatus)
	local color
	if newStatus == 0 then
		color = blue
	elseif newStatus == 2 then
		color = red
	else
		setMiddle()
		return
	end
	
	for key, value in color:GetChildren() do
		print(value.Name)
		if value:IsA("BasePart") then
			value.Transparency = 0
			value.CanCollide = true
		end
	end
end

repeat task.wait() until game:IsLoaded()

changeColors(status);

print("running2")

This doesn’t work, however, If I replace the loop with say, task.wait(1), it works. But I don’t want to wait a set amount of time in case someone loads in at a different time

1 Like

It should… Ensure that line of code is stated before the pair loop starts. If it does not, well… The parts are either just simply nonexistent or your Studio’s broken.

Make a loading screen for whatever the game is about, loading screens can also be put in intermission systems.

So this is what my folder is looking like at the moment
image
With the task.wait loop the console prints
image
which is after I call changeColors()
however with wait(1), it prints
image

Make a loading screen. Make sure it’s server-sided so it displays the same for everyone. It could load for 2-3 seconds to ensure everything has loaded, as of right now that’s the most efficient way to do so.

Then, you can use the currently working method.

What do you mean by a server-sided loading screen?

Instead of using a local script, use a legacy script. This ensures the loading screen displays on everyone’s screen at the same frame, and at the same time.

I see where you are coming from, but I’m not loading the map, this is for like when a player first joins the server

Then it could be local… It still fixes the same problem.

Wouldn’t the task.wait(1) break if the client takes more than 1 second to load all the items

Yes, if it was in the same script. If you decide to make a loading screen, make it in a separate SPS script.

And also in that case, I’m assuming you can increase the task.wait(1)

Yea but Im trying to have all this happen as soon as possible so the client doesn’t really see what is happening

Remove the line that makes the script wait and attempt to place a 0.05 task.wait() line inside of the function.

If this does not work, tell me, is the script a local script or a legacy script.

Ensure that the wait line is at the start of the function before the decompile runs for the function.