Is there a way to use FindFirstChild to find the first item which has a certain Property?

For example if I wanted to find the first Part in a Model which has its Transparency set to a certain value, is there a function like game.Workspace.Model:FindFirstChildOfWhich( “Transparency = 1” ) ? Or would I just have to just go through all of them with a in pairs loop?

You would have to iterate. The built-in ‘find’ functions can only search by name and class.

4 Likes

As the first respondent kindly says, you’ll have to iterate through. If you’re not sure what that means, it means:

use a “for i,v inpairs(your model here)” loop to iterate through the parts of the model,

then have a nested “for i,v inpairs(each part, or v above, will go here)” loop that iterates through the properties of each of those parts, searching for “Transparency”.

(oof derp)

2 Likes

I don’t think you can iterate through an object’s properties, but all you would have to do is only examine items of a desired class, since only certain types of instances have a transparency property.

I’m assuming you are searching for bricks that have transparency, so you would filter items that extend the ‘BasePart’ class:

local function findFirstChildThatIsInvisible(model)
	for _, item in ipairs(model:GetChildren()) do
		if (item:IsA("BasePart") and item.Transparency == 1) then
			return item
		end
	end
end

Some other classes, like decals, particle effects, and gui objects also have a Transparency property, which is why I checked that the item is a part in the function.

4 Likes

Ah. My bad, I should have tried it before suggesting it. Thanks for cleaning that up for me :slight_smile:
(oof derp)

1 Like