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

How would I count the bricks in my game?

Asked by 4 years ago

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

0
You want it to continuously look for parts, or just look once? KDarren12 705 — 4y

2 answers

Log in to vote
0
Answered by
KDarren12 705 Donator Moderation Voter
4 years ago
Edited 4 years ago

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.

0
Thank you neoSunny 1 — 4y
0
No problem KDarren12 705 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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()))

Answer this question