Is there a way to check if an object has a certain property?

So, i want to check if an object has a certain property.

image

for i,v in pairs(Frame:GetChildren()) do
    if v... then
        --[[ imagine there's a bunch of code about the object which has  
            "BackgroundTransparency" and "TextTransparency" property  
          ]]
    elseif v... then
        --[[ imagine there's a bunch of code about the object which has the
             "BackgroundTransparency" but not the "TextTransparency" property  
          ]]
    else
         --[[ imagine there's a bunch of code about the object which has none 
              of them
          ]]
    end
end

So, there’s a loop and i want to determine which object has the property of the “TextTransparency” and which doesn’t, via script. So script mustn’t give me an error when i want to change those properties using tween service.

There are three classes that have the TextTransparency property: TextLabel, TextButton, and TextBox. All of these classes are sub-classes of a GuiObject. Knowing that, we can check if the current index is one of those 3 classes. If it is not, then we check if is a GuiObject – as all GuiObjects have the BackgroundTransparency property. If it is not a GuiObject, then you know it doesn’t have either properties:

for i,v in (Frame:GetChildren()) do
	if (v:IsA("TextLabel") or v:IsA("TextButton") or v:IsA("TextBox")) then
		-- TextTransparency & BackgroundTransparency Tween
	elseif (v:IsA("GuiObject")) then
		-- BackgroundTransparency
	else
		-- This index has neither TextTransparency or BackgroundTransparency
	end
end
1 Like

Thanks for the answer but I don’t like using :IsA() because sometimes it makes the codes longer and i don’t want to use it for this project. So i want to ask the question another way: Is there a way to find an object using property?

Why would :IsA() make your code “longer”? It is like the shortest method and like the best way to achieve your goal
I’m guessing you could try this:

for i,v in (Frame:GetChildren()) do
	if v.TextTransparency then
		-- stuff here
	elseif v.ImageTransparency then
		-- stuff here
	elseif v.BackgroundTransparency then
		-- stuff here
	end
end
1 Like

No, there is no built in method that can check if a certain property exists on an instance. There are hacky methods that involve using pcalls or APIs but that will be much less performant compared to doing what I sent. I don’t recommend it.

1 Like

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