How can the script detect if the instance have two of the same children

Hi, so, as the title says, how can i see if a model (for example) have two of the same children (like two parts). How the script could know if there are exactly two of the same parts?

You can use a for loop to check it out.

For example,
for _ , v in pairs(model:GetChildren()) do
if v:IsA(“Part”) then … end
– or you can do
if v.Name == “name” then … end
– or any other logic you can think of
end

1 Like

You could use a for i loop to index through the model’s children, and if one is found, add 1 to a certain value. After the for i loop is done, check if the value is 2, then continue on with your code. Here’s an example for checking if a model contains only two parts:

local PartsInModel = 0
local Model = game.Workspace:WaitForChild("Model")

for Index, Part in pairs(Model:GetChildren()) do
	if Part:IsA("Part") then
		PartsInModel += 1
		
		continue
	end
end

if PartsInModel == 2 then
	print("There are two parts inside the model!")
else
	warn("There are less/more parts than expected!")
end
1 Like

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