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.
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
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!
Yeah well there are many ways to do this but yeah most used is
# 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.
(Since then Artic has now edited his post and this no longer applies)
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
Also you don’t need to post information that has already been presented.
print(tonumber(workspace:GetChildren())) instead of calling just workspace.
Slightly misread your post, sorry! Still the same though.
Also if you have anything else please contact me directly instead of posting on the thread, we are bloating the thread.
That wouldn’t work either.
:GetChildren() returns an array and so you have to do it like previously mentioned:
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.
You can do this too
Players.PlayerAdded:Connect(function() script.Parent.Text = #Players:GetPlayers() end) --you wouldn't need to call tostring here
local folder = workspace.Folder local children_count = #folder:GetChildren() print(children_count)
If you want to check the number of children use PartName:GetChildren()
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.
What if you did local Children = PartName:GetChildren()
And then print(Children)
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.
No that would only still print the array, just tested it you’d get something like this in the output.
An array is an array. A number is a number. Not the same
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
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
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)
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.
You can easily achieve this by creating a simple function and calling it whenever you need the number of children in an instance.
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
local count = Instance:GetChildren() -- you can now just use #count