Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I print how many parts are in Workspace?

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

✓ Accepted answer

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.

1 comment

0

Thanks man, this one works too. ;)

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.

4 comments

0

It isn't working, the Output says '[string "c = Workspace:GetChildren()..."]:3: attempt to call method 'IsA' (a nil value)'.

0

You have to use c[i] instead of c on line 3.

0

Now it works, Thanks man!

0

This won't scan through models in the Workspace, so it's not very useful if you have models in the Workspace

c = game.Workspace:GetChildren
if c.ClassName=="Part then"
print(#c)
end

2 comments

0

Sorry Stack, but your script doesn't really, but thanks anyway man, I appreciate it that you were to help me out though. ;)

0

k

This discussion is preserved as it was. New answers, comments, and votes are closed.