Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to Find all Children of an Instance by Class?

We do have :FindFirstChildOfClass() but It will only choose one... If I'm wrong please tell me. I want to know if there is a way to find all children from one class.

2 answers

✓ Accepted answer

That I'm aware of, there is no built-in function to do it. The best way to get all children by a class is writing a function for it yourself, which is fairly simple!

Loop through the instance's children, check their class, if it matches the one you are looking for, then add it to a table. In the end, return the table.

It's fairly simple to write

local function getChildrenOfClass(instance, class)
	-- table to hold the children
	local children = {}

	-- looping through the instance's children
	for i,v in pairs(instance:GetChildren()) do

		-- check if the child's class matches the one we're looking for
		if(v.ClassName == class)) then
			-- we add it to the table
			table.insert(children, v)
		end
	end

	-- we return the table, so that it can be used when calling the function
	return children
end

Afterwards, you can call your function whenever needed, and access it!

To access it, you can do it in the same way that you would for FindFirstChildOfClass() !

-- This will allow you to use the children you've selected using the function
local childrenOfClass = getChildrenOfClass(model, "Part")

for i,v in pairs(childrenOfClass) do
	print(v.Name) -- This would output the part's name, you can do anything else with it!
end

Documentation for tables, ClassName, loops

13 comments

1

Im not a pro in scripting, so could you refer to the returned thing?

0

Yep! I'll edit my answer to also answer that!

1

Thanks!

0

Happy to help!

0

for the "local childrenOfClass = getChildrenOfClass(model, "Part")" when is say model, "Part" do w have to change it? If so, what should we change it to.

0

Model is your Instance, but I wanted to give a practical example. "Part" is the class type, could be anything. Script, MeshPart, ShirtGraphic, anything you would like!

1

Oh ok got it!

0

Hope that this helped, if you have any further questions, let me know!

1

It worked, thanks alot!

0

Wait, how do you make it create a clone of the stuff that it finds and put in a player?

0

Using :Clone() and parenting the cloned instance to the player; https://developer.roblox.com/en-us/api-reference/function/Instance/Clone

1

Do note that IsA will return true for any object that inherits that Class too

0

My bad, will edit to use .ClassName, ty!

function GetChildrenOfClass(Instance, Class)
	local ChildrenOfClass = {}
	for _, Child in pairs(Instance:GetChildren()) do -- iterate through table returned by GetChildren
		if Child.ClassName == Class then -- Check if ClassName matches Class Argument
			table.insert(ChildrenOfClass, Child) -- insert into table
		end
	end
	return ChildrenOfClass -- Return the table
end

This discussion is preserved as it was. New answers, comments, and votes are closed.