How do I know when all descendants of Workspace are loaded in?

I added a lot of stuff to my game and I’ve noticed that my scripts are breaking because not all necessary descendants are loaded into the game when they are running.

These scripts are in StarterPlayerScripts.

I just ran a quick test, 2526 descendants are loaded when the scripts first run and search for their parts. 10 seconds later its 13851. This is literally going to destroy my game if I dont find a solution. I’ve tried game.isLoaded but it returns true even when not everything i sloaded

1 Like

Can’t you use :WaitForChild?

1 Like

The descendants of the folder I’m trying to loop through are arbitrary.

Could you show some code?

local function AddRegionsToChilds(Parent:Folder)

	for _, Folder:Instance in Parent:GetChildren() do
		if Folder:IsA("Folder") then
			print(Folder:GetChildren()) --the chlidren literaly do not exsist when this loop is ran
			for _, Part: Instance in Folder:GetChildren() do
				if not Part:IsA("BasePart") then continue end

				RegionHandler.CreateZone(Folder.Name, Part.Name, Part)
			end
		end 
	end
end

Add a .ChildAdded event.

Code:

local function AddRegionsToChilds(Parent:Folder)
	for _, Folder:Instance in Parent:GetChildren() do
		if Folder:IsA("Folder") then
			local function onChildAdded(Part)
				if not Part:IsA("BasePart") then continue end

				RegionHandler.CreateZone(Folder.Name, Part.Name, Part)
			end

			Folder.ChildAdded:Connect(onChildAdded)
			
			for _, Part: Instance in Folder:GetChildren() do
				onChildAdded(Part)
			end
		end 
	end
end

You are a LIFE SAVER. How did I not think of this before :sob:
Another win for the Dev Forum for saving me. Thank you

1 Like

No problem. If you need any other help, feel free to ask.

1 Like

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