johnhugh
|
  |
| Joined: 26 Mar 2009 |
| Total Posts: 1971 |
|
|
| 07 Mar 2012 07:36 PM |
What I need is a script that counts how many children are in a part. Say I have "part" and it has 3 scripts in it as children. I want a script that will say 3 because there are 3 children in the part, and if there were 4 children then I would want it to say 4.
- Just saying a function would be fine (if there is one). - Otherwise, if you could give me a few lines of script, I'd be fine. - Also, a link to the wiki would be good.
I'm a moderate Lua script writer, so I can handle the information I get!
If I didn't explain what I wanted well enough, please ask me to clarify.
*Thankyou!* :)
|
|
|
| Report Abuse |
|
|
|
| 07 Mar 2012 07:52 PM |
Ah, didn't know what to do, so had to experiment for awhile. x.x
But this is what I came up with:
for _, v in pairs(game.Workspace.Part:GetChildren()) do if v:IsA("Script") then print(#"v") end end
It will loop through "Part" a child of Workspace, and if they are a script, print the number of scripts. Edit as needed and tell me if it errors.
† KMXD † |
|
|
| Report Abuse |
|
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
| |
|
xvgigakid
|
  |
| Joined: 22 Jun 2008 |
| Total Posts: 4407 |
|
|
| 07 Mar 2012 07:55 PM |
Or you could do this :/
blah = object:GetChildren() NumberOfChildren = #blah |
|
|
| Report Abuse |
|
|
|
| 07 Mar 2012 07:55 PM |
Oh crap. Just tested it with multiple scripts in it, and it prints "1", however many scripts there are... Let me experiment some more. . .
† KMXD † |
|
|
| Report Abuse |
|
|
xvgigakid
|
  |
| Joined: 22 Jun 2008 |
| Total Posts: 4407 |
|
|
| 07 Mar 2012 07:56 PM |
| late toasted yet again by SDuke >.< |
|
|
| Report Abuse |
|
|
|
| 07 Mar 2012 07:56 PM |
Or let others help. D:
† KMXD † |
|
|
| Report Abuse |
|
|
johnhugh
|
  |
| Joined: 26 Mar 2009 |
| Total Posts: 1971 |
|
|
| 07 Mar 2012 07:57 PM |
Lol So define # for me? Does it count amount of children found? what about in other cases? Just trying to learn :3 |
|
|
| Report Abuse |
|
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
|
| 07 Mar 2012 07:58 PM |
| It gives the number of indexes in an array, or if used on a string is the same as `string.len()`. |
|
|
| Report Abuse |
|
|
xvgigakid
|
  |
| Joined: 22 Jun 2008 |
| Total Posts: 4407 |
|
|
| 07 Mar 2012 08:02 PM |
@SDuke `` <== Canadian French font. É Ctrl+Shift broski. |
|
|
| Report Abuse |
|
|
johnhugh
|
  |
| Joined: 26 Mar 2009 |
| Total Posts: 1971 |
|
|
| 07 Mar 2012 08:06 PM |
Thanks! Could you also give me a rundown on [i], i? You guys are really helpful btw :) |
|
|
| Report Abuse |
|
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
|
| 07 Mar 2012 08:08 PM |
[] is used for defining an index in a table. i is a variable.
http://wiki.roblox.com/index.php/Tables |
|
|
| Report Abuse |
|
|
johnhugh
|
  |
| Joined: 26 Mar 2009 |
| Total Posts: 1971 |
|
|
| 07 Mar 2012 08:12 PM |
| Well, I see i alot in brackets. What's i the default variable for? |
|
|
| Report Abuse |
|
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
| |
|
xvgigakid
|
  |
| Joined: 22 Jun 2008 |
| Total Posts: 4407 |
|
|
| 07 Mar 2012 08:15 PM |
@SDuke
Well actually the default for any variable is nil :/ |
|
|
| Report Abuse |
|
|
johnhugh
|
  |
| Joined: 26 Mar 2009 |
| Total Posts: 1971 |
|
|
| 07 Mar 2012 08:16 PM |
Hmm, well thanks guys! :) I appreciate the help! I learned what [, ], and # are used for! :)
*Thanks!* |
|
|
| Report Abuse |
|
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
|
| 07 Mar 2012 08:19 PM |
@xv
> i is just a variable.
where did I say it wasn't? |
|
|
| Report Abuse |
|
|
xvgigakid
|
  |
| Joined: 22 Jun 2008 |
| Total Posts: 4407 |
|
|
| 07 Mar 2012 08:23 PM |
@John
Ok im gonna try and explain this as simply as possible.
First you have a table Table = {"Hi", "Bob", "Noob"}
Now you print the variables in the Table like so.
print(Table[1]) >"Hi"
print(Table[2]) >"Bob"
print(Table[3]) >"Noob"
See a pattern?
Now when you use GetChildren() It creates a table, And it indexes a variable for each of those objects.
Ex. x = Brick:GetChildren() x = {Brick.Pie, Brick.Cake, Brick.Builderman}
Now for loops do the following, You give them an ammount of times to run, for example,
for i = 1, 10 do print(i) end
Now on the first loop this loop will print 1 On the second loop it will print 2 On the third it prints 3 And so on.
When we do this with tables, it loops it from 1 to the number of variables in a table.
So when we do this,
Table = {"A", "B", "C"} for i = 1, #Table do --It will loop 3 times due to their being only 3 values in the table. print(Table[i]) --You probobly see the pattern by now. end
And were done! |
|
|
| Report Abuse |
|
|
xvgigakid
|
  |
| Joined: 22 Jun 2008 |
| Total Posts: 4407 |
|
|
| 07 Mar 2012 08:24 PM |
@SDuke
Im just pointing out the fact that the default is nil. |
|
|
| Report Abuse |
|
|