Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I print the Max number of Parts in workspace?

Asked by 10 years ago

I want to make a script where it Prints how many Parts are in workspace, but I am not too sure how to, the reason I am asking is because some of my games (Even inactive ones) have too many Parts, Models, and Scripts in them that I can't tell nor keep track of them. From an older question I asked about the #, if I were to do a script using it, would this script work?

function scan(check)
print(#check:children())
end
scan(workspace)

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

There's not a maximum number of parts. Several years ago, you generally wanted to avoid more than 4000 parts in a place because of the time it took to launch the game and the power required behind to run the graphics of it. Now, it's considerably more.

What you have will work as a beginning, although is designed poorly (in the sense of "good code" vs "bad code")

The "good" version of this would look like this:

function scan(check)
    return #check:GetChildren();
end
print(scan(workspace));

This change lets us use scan for other things other than just the first thing that came to mind (printing). Also note that we use GetChildren instead of children now, just because that is the modern version of it. As far as I know there is no functional difference.

However, this will only count the children of the Workspace. If we have a group model of parts, that will contribute only 1 to the count of parts. We need to use a recursive approach. Specifically:

number of objects in model = number of children in model + number of objects in each child

We can write that like this:

function count(model)
    local number = 0;
    local children = model:GetChildren();
    for _,child in pairs(children) do
        number = number + count(child) + 1;
    end
    return number;
end

Note that this counts all objects, not just parts; including models, decals, meshes, scripts, lights, etc. If we only wanted to count parts we could do this:

function count(model)
    local number = 0;
    if model:IsA("BasePart") then
        number = 1;
    end
    local children = model:GetChildren();
    for _,child in pairs(children) do
        number = number + count(child);
    end
    return number;
end

Alternatively, you can use (via command line only) this built in method to do it. (and game:ClearMessage() to wipe it away)

Ad

Answer this question