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

The amount of children in a model?

Asked by 7 years ago

Is there a way you can find out the number of a children in a object through scripting . I heard about this a long time ago and I think it might have something to do with index but I am not sure.

0
No JJay786 -2 — 7y

2 answers

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

The pound!

By using the pound sign (aka, the # operator), you can extract the length of any array by using it right before that table. This will return a number that represents how many elements are inside the array (it's important that your table is in array format, as using this operator on dictionaries will result in a constant return of 0)


You can use this operator on the array that GetChildren returns. This will effectively show you how many objects are inside the one you just called the method on. Here's an example:

local numberOfObjects = object:GetChildren()
print(#numberOfObjects) -- Using the pound sign before the array, which will return the number of how many objects are inside of it.

-- You could also just use the operator directly before the method call, like so:
print(#object:GetChildren()) -- Does the same thing.

Also note that if you're using this operator on an array with inconsistent keys, it won't return the array's true size. Example:

local array = {}

array[1] = "one" -- Consistent so far
array[3] = "three" -- Not consistent
array[5] = "five" -- Not consistent (but at this point it doesn't matter, since the sequence was interrupted already).

print(#array) -- This will only print 1, since your array's numeric keys were consistent up to that point.

Fun fact

Just a fun fact, you can also use this operator with string values! Just use it right before a string value, and it will return the number of characters in that string. An alternative to doing this is a function in the string library called string.len, which does the same thing as #string. Here's an example:

local text = "Hello world!"
print(#text) -- > prints out how many characters were in that string

-- Same thing with string.len

print(string.len(text)) -- > Same result.

Well, that's about it. Hoped this helped, let me know if you have any questions.

Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

You should use the wiki for questions like this. Wiki: wiki.roblox.com

local numchildren = #object:GetChildren() -- use # which also means number and then the built-in roblox function :GetChildren() returns all the children inside the object meaning the number of children inside the object

Answer this question