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

How would I count the bricks in my game?

I have tried to get all the parts and have the variable for the count incrémented but it does not work, i have no code to show at the moment

1 comment

0

You want it to continuously look for parts, or just look once?

2 answers

✓ Accepted answer

This is easy with using for blank,blank in pairs do.

for i,v in pairs(game:GetDescendants()) do
wait(.1)
if v:IsA("Part") then
print("Parts") -- this will add up a number next to the print in the console (Parts(xnumberofparts)
end

I edited my question so it will not print infinitely.

2 comments

0

Thank you

0

No problem

Get the script to loop it through the parts and add up a value each time it is a base part

local CountedBricks = 0 --Variable to count how many bricks.

for _, v in pairs(game:GetDescendants) do --Every thing in the game
	if v:IsA("BasePart") then -- If it is a part
		CountedBricks = CountedBricks  + 1 --If it's a part it will add 1 to the variable.
	end
end

print(CountedBricks)

---------
Function
---------

function CountBricks()
	local CountedBricks = 0 --Variable to count how many bricks.
	
	for _, v in pairs(game:GetDescendants) do --Every thing in the game
		if v:IsA("BasePart") then -- If it is a part
			CountedBricks = CountedBricks  + 1 --If it's a part it will add 1 to the variable.
		end
	end
	
	return(CountedBricks) 
end

How would you use the fucntion? for instance you could do : print(tostring(CountBricks()))

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