Well, to get the number of elements in a table, you must you #Table, which returns highest numerical index in a table.
Also, to get only parts, you must do instance:isA'BasePart', with BasePart meaning all part classes.
Example script:
local Parts={};
for _,Object in next,workspace:children()do
if(Object:isA'BasePart')then
table.insert(Parts,Object);
end;
end;
print(string.format('Number of parts in the Workspace: %d',#Parts));
If you wanted to get every part in the workspace, even parts that are in models, you would have to use recursion.
Example #2:
local Parts do
local Recursion;Recursion=function(Object)
local Children={};
for _,Child in next,Object:children()do
if(Child:isA'BasePart')then
table.insert(Children,Child);
end;
for _,Descendant in next,Recursion(Child)do
table.insert(Children,Descendant);
end;
end;
return(Children);
end;
Parts=Recursion(workspace);
end;
print(string.format('Number of parts in the Workspace: %d',#Parts));
If you want to make a list of all the parts that are named a certain string, just have a table for containing them.
Example #3:
local Parts={};
do
local Recursion;Recursion=function(Object)
local Children={};
for _,Child in next,Object:children()do
if(Child:isA'BasePart')then
table.insert(Children,Child);
end;
for _,Descendant in next,Recursion(Child)do
table.insert(Children,Descendant);
end;
end;
return(Children);
end;
for _,Part in next,Recursion(workspace)do
Parts[Part.Name]=Parts[Part.Name]or {};
table.insert(Parts[Part.Name],Part);
end;
end;
for PartName,arrayOfParts in next,Parts do
print(string.format('Number of parts named %s in the Workspace: %d',PartName,#arrayOfParts));
end;
2 comments
It works, Is there any way to see how many are left of a Specific Name of Part?
Yes, I'll edit my answer right now.