Test for a property

How can I test a instance if it has a TextureId,MeshId,SoundId Property?

I believe you can just do something like:

if object.TextureId then
--code
end

if the property doesn’t exist it should return nil, which the if statement will skip over. Correct me if I’m wrong more advanced people, but I won’t be able to remove this post until tomorrow so keep that in mind if I am wrong.

@Meisrcool An exception of “TextureId” is not a valid member" will be thrown for nonexistent members. Always.

@ OP you will unfortunately have to write your own function to determine this.

local function instance_has_property(instance, property)
    local has_property, data = pcall(function()
        return instance[property]
    end)

    return has_property and not instance:FindFirstChild(property) and typeof(data) ~= "RBXScriptSignal" and typeof(data) ~= "function"
end
1 Like

Basically I need to grab all the Ids of all the assets already in the workspace, and there is a ton of them. There’s Textures, MeshParts, Meshes, and Sounds…

I want to write a script that can simply go through all of them and print the values if the descendent has a id property.

Why can’t you just check like this:

if obj:IsA("Texture") or obj:IsA("Sound") then end
-- for whatever objects you've got

in one loop, O(n).