How to check if an Instance has Certain Properties?

I want to find a way to identify if an Instance Has Certain Properties, for example:
Rather than doing this :

for i,v in pairs(script.Parent:GetDescendants()) do
		if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("UnionOperation") or v:IsA("Decal") then
			-- Do Something --
		end
	end

Maybe i can just check if an Instance has Properties Called Transparency, if it has then set the transparency to 1
i tried to use something like

if v.Transparency ~= nil then
-- Do Something--
end

if v.Transparency then
-- Do Something
end

but it didnt work, so maybe someone know how to check if an Instance has Certain Properties. if you do know how to do this, Please Reply!

15 Likes

Perhaps a way to do this is, using pcall. You can do some hacky trickery, by calling a function that’s supposed to just do object.Transparency, if Transparency didn’t exist, pcall will return false (because the function errored), if it existed it returns true (because it functioned correctly).

local function hasProperty(object, prop)
    local t = object[prop] --this is just done to check if the property existed, if it did nothing would happen, if it didn't an error will pop, the object[prop] is a different way of writing object.prop, (object.Transparency or object["Transparency"])
end

for i,v in pairs(script.Parent:GetDescendants()) do
   local success = pcall(function() hasProperty(v, "Transparency") end)) --this is the part checking if the transparency existed, make sure you write the property's name correctly

   if success then
    --the rest of your code
   end
end

All though I do think that this is quite costly, and you can simply reduce your original code by this, since Unions, Mesh Parts, Parts… All have one common class which is the "BasePart" class. (the decal class gotta say though)

if v:IsA("BasePart") or v:IsA("Decal") then
 -- Do Something --
end
52 Likes

Instead you can do :IsA(“BasePart”) because all parts inherit from basepart.

for i,v in pairs(script.Parent:GetDescendants()) do
		if v:IsA("BasePart") or v:IsA("Decal") then
			-- Do Something --
		end
	end```
6 Likes

If you need to run this multiple times, you could probably also place these objects in to their own tables bas based on their class. Instead of doing GetDescendants()

2 Likes

See the following thread for some tips on creating a property finder method:

3 Likes

Came here with the same question.

Instances like Part and Decal both have a Transparency property. I want to set them. I have no idea how to test for the existence of this field, but it seems pretty basic.

Like the answer above concedes, I have no way of distinguishing a child Transparency instance from a property with Part[“Transparency”], except maybe if I also check Part[“Transparency”].Parent = nil. Seems convoluted.

9 Likes

Did you ever find a solution? I’m having the same issue rn

1 Like

Same here. I want to check if a part has the attribute i gave it

Found the solution for the properties thing but i’m using promises. Do you want it?

please yes it would make my day

local Knit = require(game:GetService("ReplicatedStorage"):WaitForChild("Knit"))
local Players = game:GetService("Players")
local DataStructure = require(script.DataStructure)
local Promise = require(Knit.Util.Promise)
local TableUtil = require(Knit.Util.TableUtil)

local PlayerService = Knit.CreateService {
    Name = "PlayerService";
    Client = {};
}

local PlayersTable = {}

function ObjectHasPropertyPromise(object:Instance, prop)
    return Promise.new(function(resolve, reject, onCancel)
        local t = object[prop]
        resolve()
    end)
end

local function PlayerAdded(Player:Player)
    if PlayersTable[Player] == nil then
        local PlayerTable = TableUtil.Copy(DataStructure)
        for Key, Value in pairs(DataStructure) do
            ObjectHasPropertyPromise(Player, Key):Then(function(Value)
                PlayerTable[Key] = Player[Key]
            end):Catch(function()
                PlayerTable[Key] = Value
            end)
        end
        if Player then
            PlayersTable[Player] = PlayerTable
            print(PlayersTable[Player])
        end
    end
end



function PlayerService:KnitInit()
    Players.PlayerAdded:Connect(PlayerAdded)

    for _, Player in pairs(Players:GetPlayers()) do
        task.spawn(function()
            PlayerAdded(Player)
        end)
    end
end


return PlayerService