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

How can I print how many parts are in Workspace?

Asked by 10 years ago

I tried to make a script where it prints the amount of bricks in workspace, but they dont do nor work as their supposed to, here are the scripts

 c=workspace:children()
for i=1,#c do
if c.className=="Part"then
print(c)
end end

That didn't print anything, so I then tried this;

c=workspace:children()
for i,v in ipairs(c)do --Did 'pairs' too
if v:IsA("Part")then
print(v)
end end

It only print the names of the Brick(s), what am I doing wrong?

3 answers

Log in to vote
1
Answered by 10 years ago

This is a script that scans through the descendants of the Workspace and adds those bricks to the list too, instead of just scanning the Workspace

local PartCount = 0
function GetPartCount(Obj)
    for _,v in pairs(Obj:GetChildren()) do
        if v:IsA("BasePart") then
            PartCount = PartCount + 1
        end
        GetPartCount(v)
    end
end
GetPartCount(game.Workspace)
print("There are "...PartCount.." parts in the Workspace!")

This will scan through models in the workspace, and the models of those models, and so forth.

0
Thanks man, this one works too. ;) TheeDeathCaster 2368 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

If you only want the personal copy, I recommend using a NumberValue in Workspace, then doing this:

c = Workspace:GetChildren()
for i = 1, #c do
    if c:IsA("Part") then
        game.Workspace.NumberValue.Value = game.Workspace.NumberValue.Value + 1
    end
end

I'm Aurum, and you're welcome.

0
It isn't working, the Output says '[string "c = Workspace:GetChildren()..."]:3: attempt to call method 'IsA' (a nil value)'. TheeDeathCaster 2368 — 10y
0
You have to use c[i] instead of c on line 3. gskw 1046 — 10y
0
Now it works, Thanks man! TheeDeathCaster 2368 — 10y
0
This won't scan through models in the Workspace, so it's not very useful if you have models in the Workspace TurboFusion 1821 — 10y
Log in to vote
-2
Answered by 10 years ago
c = game.Workspace:GetChildren
if c.ClassName=="Part then"
print(#c)
end
0
Sorry Stack, but your script doesn't really, but thanks anyway man, I appreciate it that you were to help me out though. ;) TheeDeathCaster 2368 — 10y
0
k stackyjack 0 — 10y

Answer this question