Is it possible to somehow filter what instances :GetDescendants() returns?

I have this script that tags assets using CollectionService so that my loading screen knows what to preload.

local CollectionService = game:GetService("CollectionService")

function isAsset(v)

	if v:IsA("Decal") or v:IsA("ImageLabel") or v:IsA("ImageButton") or v:IsA("MeshPart") or v:IsA("SpecialMesh") or v:IsA("Texture") or v:IsA("Sound")then
		return true
	end
	return false
end

for i,v in pairs(workspace:GetDescendants()) do 
	
	if isAsset(v) then
		CollectionService:AddTag(v, "Asset")
	end

end

for i,v in pairs(game.ReplicatedStorage:GetDescendants()) do 

	if isAsset(v) then
		CollectionService:AddTag(v, "Asset")
	end

end

for i,v in pairs(game.StarterGui:GetDescendants()) do 

	if isAsset(v) then
		CollectionService:AddTag(v, "Asset")
	end

end

for i,v in pairs(game.MaterialService:GetChildren()) do
	
	if v:IsA("MaterialVariant") then
		CollectionService:AddTag(v, "Asset")
	end
	
end

--print("tagged assets")

game.ReplicatedStorage.GotAssets.Value = true

I know using :GetDescendants() on the workspace is horrendous for preformance, so I’m wondering: Is there a way to filter what it returns? Or at least a more efficient way of looping through everything?

Definitely a few ways one could do this.

Is it possible to tag the instances in Studio rather than at runtime? Similarly, what about tagging these assets more selectively (if you are creating/cloning them via script)?

I could tag them in Studio, the main issue is that I’m worried I’ll forget to do it sometimes when I update the game.

I guess I could write a plugin that tags everything for me in Studio.

Yeah that’s fair.

Maybe this is more of a fundamental question - but do you actually need to preload all of these assets? Is there a starting area / core elements that you could instead prioritize (less overhead for clients too)?

1 Like

Yeah, that’s a good idea. Don’t know why I didn’t think of that.

Rad - would recommend it. Less burden on clients and should make tagging it a roughly one-time thing.

Again, lots of ways you could solve this, but the one that works best is whichever one is the easiest for you to upkeep and (probably) has the least :GetDescendants() calls.

1 Like

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