How can I check if an instance is an asset? (Please read fully first before replying)

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I have a loop of instances and I want to filter out which ones are assets. What I mean by assets are like sounds, images, animations and what not. You guys get the idea right?

  2. What is the issue? I’m unable to filter out the instances and get the assets only.

  3. What solutions have you tried so far? I’ve tried looking all over the documentation/devforum and the closest thing I could find was :GetEnumItems() which I couldn’t really use, I tried also type checking but obviously assets are instances aswell.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local function AddPlayerAssets()
	for Every, Asset in game:GetDescendants() do
		for Every, ConfirmedAsset in Enum.AssetType:GetEnumItems() do
			if Asset == ConfirmedAsset then
             -- I know this wasn't gonna work but yeah couldn't think of anything else to check
             -- Print statement doesn't print obviously meaning it didn't work

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category. *I’m only asking for the thing I can use to filter this out guys, I don’t need an entire system I want to learn aswell. That’s how we become better.

Please read this
I do have two solutions in mind but they’re really ineffective, the first is to just make a bunch of or statements in an if statement and literally write out all the assets possible which will be tiring but would work. I have a second solution too but I can’t remember it but I wrote it down somewhere, it’s better than the first solution that I stated but still might not be the best route.

You can create a set of all the class names you’d like to filter out. e.g., ("Animation", "Sound", ...). Then in your for loop check if the instance’s class name is in that set.

But I want to preload all types of assets, the thing is the game is really big so there are many different types of assets and it’d be really hard to do that. I was thinking about doing this originally too but it’d be very time consuming and inefficient. Unless there are any alternatives?

It actually was my first idea and I didn’t mean what I wrote literally, I meant I was gonna do something like

if Asset:IsA("example") or -- Repeat these things

Oh I’m not really saying write out a bunch of if or statements. Moreso something along the lines of

-- This is a lua version of a "set" with quick look ups
local AssetSet = {
    Animation = true,
    Sound = true,

-- in loop
if AssetSet[Instance.ClassName] then
   -- We found an asset we wanted to filter

I think in this way, it’s not inefficient nor time consuming. You write out the set once and you can do O(1) look ups in the set when looping through all the game instances. Modifying the set to remove or add types should be pretty quick.

Is there no alternative? The documentation doesn’t have like a built in feature to check this? If that’s the case then they should add one this still is a bit inefficient but it’s good and the best option I think I have so thank you!

As far as I know, there isn’t a built in API that does what you want.

The closest thing I can think of would be to use CollectionService | Documentation - Roblox Creator Hub. GetTagged will return an array of all the instances with the specified tag. But this means you’d either need to manually add the tags yourself In Studio to all the instances you consider an asset, or make a script similar to the above to add the tags programmatically. At that point though, I feel like you’re accomplishing the same thing with extra steps so it’s probably not worth it.

Yeah I’m taking your idea, thanks.

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