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

Get Print-Only Object List?

Asked by 10 years ago

This is bit hard to explain but... what method would i have to type to get a print-only version of the amount of objects a model has inside of it?

For instance, if i have a model in Workspace that contained 2 Parts, What would i type to get that value? Ty for the help. :)

2 answers

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

The best solution to this would be recursive, so that we count objects inside of sub-models (as opposed to just direct children of the model we are talking about).

Here is our recursive statement:

objects in model = #objects as direct children + #objects in each child

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

If there was a model of 5 parts in the workspace called "thing", then we could get that 5 using:

countParts(workspace.Thing)
Ad
Log in to vote
0
Answered by
TofuBytes 500 Moderation Voter
10 years ago

Just place this script into the model. This will work. :)

    local parts = 0
    local stuff = script.Parent:GetChildren()
        for i = 1, #stuff do
            if stuff[i].ClassName == "Part" then
            parts = parts + 1
            end
        end
    print("Parts: "..parts)
0
`:IsA("Part")` is more robust; especially because you can use `:IsA("BasePart")` and also count SpawnLocations, TrussParts, WedgeParts, etc BlueTaslem 18071 — 10y
0
Ah ok, thank you. :) TofuBytes 500 — 10y

Answer this question