Counting children

Let’s say I have a folder with 3 parts in it and I wanted to make a storage system that you can only hold 5. Is there a way for any script to count the children in said folder and then print it?

3 Likes

Yes there is using the # character

#FOLDERHERE:GetChildren() -- # returns the number of arguments in the GetChildren table
7 Likes

Sorry if I sound stupid to you, but can you give me a bit more of an explanation?

2 Likes

You can use a for loop if you want:

local numberofchildren = 0

local folder = game.ReplicatedStorage.Folder --Change this to your folder

for _, number in pairs(folder:GetChildren()) do -- this is going to loop according to how many childs you have for example you have 3 childs its going to loop 3 times
	numberofchildren = numberofchildren + 1 -- each time we loop we add 1 to the vairible
end
wait(2)

print(numberofchildren) -- we print the number of children
3 Likes

You can use ChildAdded function when a child is being added inside of the folder is going to increase the number of child inside of a folder also if child is being removed you can use ChildRemoved function.

local Folder = script.Parent
local Count = Folder:FindFirstChild("IntValue") -- Make sure you create one IntValue

Folder.ChildAdded:Connect(function()
   Count.Value = Count.Value + 1
end)

Folder.ChildRemoved:Connect(function()
   Count.Value = Count.Value - 1
end)

7 Likes

Sure thing

:GetChildren() returns a table of all the direct children of the object specified.
The # character when used in front of a table returns the number of arguments in a table.

Effectively #Folder:GetChildren() is just the number of children in the specfied object

So for your code you could do something like this:

local NumChildren = #FOLDERHERE:GetChildren()

if NumChildren > 3 then
   --we have more than 3 children in the folder
end
5 Likes

Basically you use # to get a number of the instance that has been inputted. Here is an example for better understanding.

-->> Variables
local Folder = -- path

-->> Main Code
print(#Folder:GetChildren()) -- it would print out all the children of the folder
4 Likes

The length operator returns the number of bytes for strings.

I’m sure that’s true but it also works for tables, which is perfect for this situation.

The developer page lists the # operator as the “Length of table”

1 Like
local num_children = table.getn(object:GetChildren())

:GetChildren() returns all the children as a table.
# returns the amount of things in a table.

So if you were to do #folder:GetChildren(), it would return the amount of children inside the folder.

Example:

local folder = script.Parent
local count = #folder:GetChildren()

folder.ChildAdded:Connect(function()
    count = #folder:GetChildren()
end)

folder.ChildRemoved:Connect(function()
    count = #folder:GetChildren()
end)
1 Like

To be more accurate (and avoid confusion on your side), here’s a full explanation:

Source taken from Lua 5.1 Reference Manual

why not just count += 1 and count -= 1 when a child is added and removed?

That’s actually referring to bytes of characters. A character is just one letter which has a byte of 1. So if you get an array of characters which is also called a string, the length of the string would equal the amount of bytes. However, that is only the case if all elements of an array are characters.

Edit: Did not see that you stated “for strings”. I thought you were referring to cases when elements of an array are not just characters.

Those are more recently added operators, and a lot of people don’t know about them.

What prevents OP from just doing a = a + 1 then…?

Both ways are fine in the end, whether you wanna do #table:GetChildren() each time or count +=/-= 1, it’s just a matter of preference afaik.

I’d say it’s having optimized code, but sure

1 Like

Nothing. I’m just saying they’re still kinda new operators for Roblox and some people don’t know about them.

1 Like

I’m sorry for the misunderstanding; I was meaning that simply adding / subtracting one number from the count is much more efficient than performing a function call each time. I used += & -= as I quickly wrote that code up.

3 Likes