:FindFirstChild() on multiple instances

Is it possible to use :FindFirstChild() to look for multiple instances and if so, how would this be phrased? If not is there an alternate method to detect multiple instances at once?

If you’re referring to multiple instances of the same name, then yes.

for i, v in pairs(workspace:GetChildren()) do
	if v.Name == "" then
		--do some code
	end
end

This gets all the children of the workspace & if they match a particular name some code is executed.

for i, v in pairs(workspace:GetChildren()) do
	if v:FindFirstChild(" ") then
		--do some code
	end
end

I think he meant to find something inside the object not check the object itself.

Isn’t finding something within an object checking the object? Unless he was looking for a script which looks for multiple instances of the same class in which case he will need to use the following.

for i, v in pairs(workspace:GetChildren()) do
	if v.ClassName == "" then
		--do some code
	end
end

Yes, but it is easier to loop through everything in the workspace and then check if it contains something rather than looping through everything and then putting another if statement to check if it is there. If the part is something that’s located in multiple objects and wanted only a few with a certain name, then your code would work more efficient.

:FindFirstChild() and .Name do exactly the same thing, both checks for an object of a specified name.

Basically I’m trying to use :FindFirstChild() but to check if multiple instances exist instead of only one. If they do, the next block of code will run.

FindFirstChild() finds the object inside the object, Name only finds the object.

Right, but you can just change the path to match the object.

for i, v in pairs(workspace.somemodel.object:GetChildren()) do
	if v.Name == "" then
		--do some code
	end
end
for i, v in pairs(workspace.someobject:GetChildren()) do
	if v:FindFirstChild("") then
		--do some code
	end
end

Are equivalent.

But I’m saying it is easier to use FindFirstChild instead of adding another loop to check for the object. They are equivalent but FindFirstChild makes the code less confusing with less code when you’re typing out a big program.

There is no extra loop, both implementations only make use of a single generic for loop.

local dict = {}

for i, v in pairs(workspace.ModelName:GetChildren()) do
	if dict[v.Name] then
		dict[v.Name] += 1
	else
		dict[v.Name] = 1
	end
end

for i, v in pairs(dict) do
	if v > 1 then
		--do some code
	end
end

This will go through all of the children of an object, swap “ModelName” to whatever the name of your model/object is. For all instances which occur two or more times some code is executed.

1 Like

That is if you have a table.

is the same thing as v:FindFirstChild("") but what you’re loop is doing is getting the Name of the Instance and then getting the children of the Instance by using v.Name. But :FindFirstChild() is directly getting the Instance’s name from the object.

v.Name does not get the children of an instance. A second table is necessary to count the instances which occur two or more times. A working solution has been provided above

:FindFirstChild() gets the children, so it is easier to just do :FindFirstChild() of doing

if v.Name == "Part" then
    -- Another loop to get the children of that part.
end
if v:FindFirstChild("Part") then -- This finds the part inside the Instance whereas the previous one finds the part name and then finds the part inside the Instance.
    -- Code
end

.Name and FindFirstChild() both find an instance using its name. They are essentially the same thing. Similarly to how .ClassName and FindFirstChildOfClass() both find an instance from its class.

You can try something like this:

function findFirstDescendants(parent, name)
   --function to find children of a specific name
   local descendants = {}

   for _, child in ipairs(parent:GetChildren()) do
      --loops through the children to find one of a specific name
      if child.Name == name then
         table.insert(descendants, child)
      end
   end

   return descendants --returns a table for easy access
end

--example usage
if #findFirstDecendants(workspace.Part, "Script") > 0 then
   print("Found children named 'Script' !")
end

In what case? The child with the same name or?

if workspace.Model.Part.Name == "Part" then
	--do code
end

if workspace.Model:FindFirstChild("Part") then
	--do code
end

Notice how both achieve the same desired result?