|
| 21 Jun 2012 05:35 PM |
local x = variable1:GetChildren() for i = 1,#b do
Now how do I make it get a random child every time I run the script? |
|
|
| Report Abuse |
|
|
|
| 21 Jun 2012 05:37 PM |
local x = variable1:GetChildren() for i = 1,#b do thatnum = math.random(1, #b) --stuff end
- Striked |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 21 Jun 2012 05:37 PM |
This is how I do it.
local tab = {} for i,v in pairs(variable1:GetChildren()) do table.insert(tab,v.Name) end seed = tab[math.random(1,#tab)]
|
|
|
| Report Abuse |
|
|
|
| 21 Jun 2012 05:39 PM |
Thanks
Also just noticed that I had the variable be x in one place and b in the other :/ |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 21 Jun 2012 05:47 PM |
| I also noticed you didn't have an end, but hey! There's a reason how you didn't finish the script. So we don't have to worry I guess. . . |
|
|
| Report Abuse |
|
|
|
| 21 Jun 2012 06:33 PM |
@miz, kinda redundant.
c = variable1:GetChildren() for _,_ in pairs(c) do randomchild = c[math.random(#c)] end |
|
|
| Report Abuse |
|
|
|
| 21 Jun 2012 07:19 PM |
Now, if you want to avoid the possibility of picking the same element twice, try this:
------------ local x = variable1:GetChildren() for i = 1, #x do local curr = table.remove(x, math.random(#x)) print("curr == ", curr) --do stuff end ------------ |
|
|
| Report Abuse |
|
|
| |
|
| |
|
Rukiryo
|
  |
| Joined: 04 Sep 2009 |
| Total Posts: 1490 |
|
|
| 21 Jun 2012 09:25 PM |
I'd do: x = variable1:GetChildren()[math.random(1,#(variable1:GetChildren()))] That's the best way to do it in 1 line. |
|
|
| Report Abuse |
|
|
|
| 22 Jun 2012 10:07 AM |
@yack
GetChildren returns a read-only table. You can't perform manipulation on it.
@rukir
To make the least amount of characters, you can shorten it a little bit more:
x = variable1:GetChildren()[math.random(#variable1:GetChildren())]
And yeah one too many ')' |
|
|
| Report Abuse |
|
|
|
| 22 Jun 2012 05:18 PM |
| @bunny, I suggest you test my code. It works perfectly fine. Thanks, though. |
|
|
| Report Abuse |
|
|
|
| 22 Jun 2012 06:15 PM |
| That doesn't make sense. It is read-only! |
|
|
| Report Abuse |
|
|
|
| 22 Jun 2012 06:24 PM |
You guys are overdoing it.
s = b:GetChildren() x = s[math.random(#s)] |
|
|
| Report Abuse |
|
|
|
| 22 Jun 2012 07:04 PM |
@bunny, I have come to the realization that the wiki is, unfortunately, not exactly the end-all for knowledge of rbx.lua. learn2test ;-)
@king, in the OP's code, he included a for loop. Therefore, we followed suit. |
|
|
| Report Abuse |
|
|
|
| 22 Jun 2012 07:27 PM |
I did test, yak, and I am only surprised that you can manipulate a read-only table.
I am educated enough to know what read-only means, and then leave it there. |
|
|
| Report Abuse |
|
|