Counting Objects Tutorial (Roblox Studio)

Copy the code into Roblox Studio CommandBar and read the output.


things = 0 for k, thing in pairs(workspace:GetDescendants()) do
if thing:IsA("Part") then
things = things + 1
end
end
print(things .. " were found in the under game.Workspace")

Now, that does not count everything in Studio. What it does it make a variable called things and updates its value every time it finds a match. The function "IsA()" is a Instance function defined by Roblox. Instance is a top level class that most classes inherit from.

"IsA()" takes in a string and determines if the classname that is sent in matches the classname of its own class. This means that if you run the following:

print(Instance.new("Part"):IsA("Part"))

the output would be

> print(Instance.new("Part"):IsA("Part"))
true

We can do this on ParticleEmitters too! It is a efficient way to see how many particle emitters there are in parts of your game. Let's go ->

things = 0
for k, thing in pairs(workspace:GetDescendants()) do
if thing:IsA("ParticleEmitter") then
things = things + 1
end
end
for k, thing in pairs(game.ReplicatedStorage:GetDescendants()) do
if thing:IsA("ParticleEmitter") then
things = things + 1
end
end
for k , thing in pairs(game.ServerStorage:GetDescendants()) do
if thing:IsA("ParticleEmitter") then
things = things + 1
end
end
print(things)

Output for me was -> 594

I hope you learned something from this, my discord server is full of scripters and devs -> https://discord.gg/mrpzQBv