Hello, developers. A lot of my friends asked for me to make them a script that counts the total parts in their workspace. So I decided to make a script that counts all parts, unions, mesh parts, truss parts, etc.
WHAT DOES THIS DO?
This script runs a loop through the whole workspace and count’s every part, mesh part, union, wedge, truss part, and corner wedge part. When it finishes the loop it prints all of the counted parts and the total amount of parts. This is useful because it will help builders and map designers monitor the amount of parts the game has.
HOW TO USE:
You can either paste this script into the command bar and check your Output for results or you can paste this code in a local script (doesn’t matter where you put the script), play the game, and check DevConsole for results.
function GetParts()
local TotalParts = 0
local Parts = {}
for i, v in ipairs(workspace:GetDescendants()) do
if v:IsA("BasePart") then
TotalParts += 1
if Parts[v.ClassName] then
Parts[v.ClassName] += 1
else
Parts[v.ClassName] = 1
end
end
end
return Parts, TotalParts
end
local Parts, TotalParts = GetParts()
for i, v in pairs(Parts) do
print(i..": "..v)
end
You could use it but this is just an alternative way to do it. This code counts every single TYPE of part. So you can see how many unions or mesh parts your workspace has.
local BaseParts = {}
for _, a in pairs(workspace:GetDescendants()) do
if not a:IsA('BasePart') then
continue
end
BaseParts[a.ClassName] = (BaseParts[a.ClassName] and BasePart[a.ClassName] + 1) or 1
end
This code prints every specific part and the total. Your code only prints the total amount in the whole workspace, and it doesn’t check if its a part or not. So it would be printing models, folders, etc.
function GetParts()
local TotalParts = 0
local Parts = {}
for i, v in ipairs(workspace:GetDescendants()) do
if v:IsA("BasePart") then
TotalParts += 1
if Parts[v.ClassName] then
Parts[v.ClassName] += 1
else
Parts[v.ClassName] = 1
end
end
end
return Parts, TotalParts
end
local Parts, TotalParts = GetParts()
for i, v in pairs(Parts) do
print(i..": "..v)
end
print("Total: "..TotalParts)
I looked at the original function and I thought I would remake it
honestly I don’t find any use case for knowing this information, seems weird
So you can monitor games with larger maps and stuff. For example, if you have a showcase level map with like 10k unions but you arent aware, you can check it out by using the code.
How about… you seem to have forgotten how return works.
The better solution would be:
local BaseParts = {}
for _, a in ipairs(workspace:GetDescendants()) do
if a:IsA('BasePart') then
BaseParts[a.ClassName] = (BaseParts[a.ClassName] or 0) + 1
end
end
print(BaseParts)