Script that counts Total Parts in Workspace

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
11 Likes

Why not just use the Physics tab under View?

8 Likes

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.

You know you could just do v:IsA("BasePart") and just add the classname on the list of things with

cornerWedgeParts = v.ClassName == "CornerWedgeParts" and cornerWedgeParts+1
3 Likes

Yeah, this was just an experimental code. It’s not the best-structured code, it’s a complete abomination but it does the job.

How about… no.

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
6 Likes

Very useless, I’d never use. Couldn’t you just do:

print(#game.Workspace:GetDescendants())?

Or you could do workspace instead of game.Workspace.

1 Like

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.

K, but why would I need to know the parts in my workspace?

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

1 Like

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.

This is probably a better way of doing it.

Use this then.

4 Likes

why not add to “total” in the first for loop

that will error try this

parts[v.ClassName] = parts[v.ClassName] and parts[v.ClassName] + 1 or 1

or if you actually want it to look nice

if parts[v.ClassName] then
    parts[v.ClassName] += 1
else
    parts[v.ClassName] = 1
end

Like I said I wrote this in the forum editor so there were bound to be bugs. Sorry for the inconvenience.

1 Like

your fine, don’t worry about it

1 Like

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)

If you’re going to be rude, at least be right.

2 Likes

So I’m downright rude for doing criticism? Thank you for noting the error though.

this might seem rude to some, maybe tell them why the code is bad instead of that

1 Like

“Very useless, I’d never use.” Perhaps you should be a little less harsh, you know.

1 Like