How to check the number of children of an instance?

Hey there, I have a quick question which is ; How to check the number of children of an instance. I’ve looked around but I have found nothing that helps me.

22 Likes

Concept

This is pretty simple actually. All you have to do is call the :GetChildren() method on your index then get the length of the returned array using the # operand.

local children = someInstance:GetChildren()
local count = #children

print(count) 

You can do the same with :GetDescendants and other similar functions that return an array too!

Resources

https://developer.roblox.com/en-us/api-reference/function/Instance/GetChildren
https://www.lua.org/pil/2.5.html
https://www.lua.org/manual/5.1/manual.html

32 Likes

Yeah well there are many ways to do this but yeah most used is #children as # declares number of childrens. But here is another example:

local count = 0
for _,v in pairs(workspace:GetChildren()) do
	count = count + 1
end
print(count)

I hope this helps. :herb:

22 Likes

(Since then Artic has now edited his post and this no longer applies)

Hello!

If you are unsure whether your answer even works then you should maybe take the time to check whether it does. I can personally say that calling tonumber on an instance will not get the amount of children inside of it. In fact it will just return nil

image

Also you don’t need to post information that has already been presented.

5 Likes

Use print(tonumber(workspace:GetChildren())) instead of calling just workspace.

6 Likes

Slightly misread your post, sorry! Still the same though.
image

Also if you have anything else please contact me directly instead of posting on the thread, we are bloating the thread.

4 Likes

That wouldn’t work either.
:GetChildren() returns an array and so you have to do it like previously mentioned:

print(#workspace:GetChildren())
14 Likes

They’re not being selfish, in fact they are helping you. You are posting information that does not work and you are getting upset when you are corrected. Stop being childish, learn from the information they gave you.

9 Likes

You can do this too

 Players.PlayerAdded:Connect(function()
     script.Parent.Text = #Players:GetPlayers()
 end)
--you wouldn't need to call tostring here

or

 local folder = workspace.Folder
 local children_count = #folder:GetChildren()

 print(children_count)
6 Likes

If you want to check the number of children use PartName:GetChildren()

1 Like

Nope that is completely incorrect
PartName:GetChildren() would only return an array of the part’s children
to actually check the number/count , you would have to for example, use the # unary operator

  #PartName:GetChildren()--------> count of it's children i.e the size of the array
  print (#PartName:GetChildren())-----------> true

to show the total number of instances under the part.

3 Likes

What if you did local Children = PartName:GetChildren()
And then print(Children)

1 Like

you would need to do print(#Children) because GetChildren() returns an array. An array is not a number, so printing just Children would try to print the array.

2 Likes

No that would only still print the array, just tested it you’d get something like this in the output.

table: 0x92b0963a61c3af03
An array is an array. A number is a number. Not the same

2 Likes

the problem w :GetChildren() is its an array function if you’re doing something that needs you to keep track of howmany children something has frequently spamming it will cause massive lag especially if there are dozens of children what i’d do instead is using .ChildAdded and .ChildRemoved after calling #Instance:GetChildren() once that way your game wouldnt suffer from massive lag tbh when you think about it its sad that there isnt a built in function that returns the number of children or descendants an instance has i can see it helping out a ton :confused:

this is what i mean:

Amount = #Instance:GetChildren()
Instance.ChildAdded:Connect(function()
	Amount += 1
end)
Instance.ChildRemoved:Connect(function()
	Amount -= 1
end)

i dont think you can get any efficient than this feel free to correct me if im wrong

1 Like

I was wrong there is a very MINOR optimisation you could do which is ditching += and -= as they’re actually slower than a = a + 1 and a = a - 1 especially if a is a local variable so the more efficient version would be this

local Amount = #Instance:GetChildren()
Instance.ChildAdded:Connect(function()
	Amount = Amount + 1
end)
Instance.ChildRemoved:Connect(function()
	Amount = Amount - 1
end)

Edit:
If you’re using this in a local script keep in mind replication can do weird things so make sure whatever you’re connecting this to gets loaded properly before connecting it.

1 Like

You can easily achieve this by creating a simple function and calling it whenever you need the number of children in an instance.

example:

function countnumberofchildren(instance)
	local c = 0
	for i,v in pairs(instance:GetChildren()) do
		c += 1
	end
	return c
end

local numberofchildren = countnumberofchildren(mypart)
print(numberofchildren) -- for example if "mypart" had 16 children it would return as 16

Easy answer?

local count = Instance:GetChildren()
-- you can now just use #count