Best way to collect game assets in to table

local function getAssets()
	local assets = {}
	
	
	for k, v in pairs(ROOTS) do
		for k, v in pairs(v:GetDescendants()) do
			if v:IsA("Decal") or v:IsA("ParticleEmitter") or v:IsA("Texture") then
				table.insert(assets, v.Texture)
			elseif v:IsA("ImageButton") or v:IsA("ImageLabel") then
				table.insert(assets, v.Image)
			elseif v:IsA("Sound") then
				table.insert(assets, v.SoundId)
			end
		end
	end
	
	
	return assets
end

This feels wrong. Is there a better way. May not support future instances and there’s probably instances I’m missing, so bad code

The only way to optimize that is to only do it once, so you don’t have to loop over GetDescendants every time.

This function is only called once, I just wanted a better method to collect the assets instead of checking what type of class it is, instead just finding Content properties and adding them to the table

I don’t know if thats possible or if it is how to do it

Why are you making a function like this? If it’s for PreloadAsync, then it does this automatically. You can just load the ROOTS in your function.

1 Like

I load each asset individually so I can update a progress bar

Although now that I think about it I can just throw the instances themselves in there

Don’t preload everything at once - everyone loses that way. Players get long meaningless load times and you have to write something that preloads EVERYTHING, adding a skip button only increases the chance that nothing important will preload. Preload what the player will see in the first minute or so of gameplay.

1 Like

If you’re using PreloadAsync, you can pass a table of objects instead of the asset IDs:

ALSO, it will scan the descendants of the objects passed too. From the API Reference:

So you could technically just pass your ROOTS table to the PreloadAsync function:

game:GetService("ContentProvider"):PreloadAsync(ROOTS)

Edit: Just to be clear though, you should only preload assets that actually need to be preloaded (if you are preventing the player from playing until all that stuff is loaded). If it’s just preloading in the background & you aren’t holding up the user, then it’s fine. It’s marked as an “async” function, which basically means it runs asynchronously from the main thread, thus won’t slow any other scripts down.

4 Likes

ROOTS is a collection of folders with assets that are required immediately.
I’m fully aware it’s asynchronous and I’m preloading everything in ROOTS for good reason.

Thanks everyone for your help :slight_smile:

1 Like