generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: when getting children, how do you pick a random one?

Previous Thread :: Next Thread 
HillBilly9843 is not online. HillBilly9843
Joined: 05 Jan 2009
Total Posts: 7870
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
LordStriked is not online. LordStriked
Joined: 19 Mar 2011
Total Posts: 5396
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 is not online. 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
HillBilly9843 is not online. HillBilly9843
Joined: 05 Jan 2009
Total Posts: 7870
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 is not online. 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
BunnyBoy26 is not online. BunnyBoy26
Joined: 17 Jun 2010
Total Posts: 5674
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
yacker3000 is not online. yacker3000
Joined: 22 Nov 2008
Total Posts: 259
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
BunnyBoy26 is not online. BunnyBoy26
Joined: 17 Jun 2010
Total Posts: 5674
21 Jun 2012 08:40 PM
^
Nope.
Report Abuse
yacker3000 is not online. yacker3000
Joined: 22 Nov 2008
Total Posts: 259
21 Jun 2012 09:23 PM
How so not?
Report Abuse
Rukiryo is not online. 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
BunnyBoy26 is not online. BunnyBoy26
Joined: 17 Jun 2010
Total Posts: 5674
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
yacker3000 is not online. yacker3000
Joined: 22 Nov 2008
Total Posts: 259
22 Jun 2012 05:18 PM
@bunny, I suggest you test my code. It works perfectly fine. Thanks, though.
Report Abuse
BunnyBoy26 is not online. BunnyBoy26
Joined: 17 Jun 2010
Total Posts: 5674
22 Jun 2012 06:15 PM
That doesn't make sense. It is read-only!
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
22 Jun 2012 06:24 PM
You guys are overdoing it.

s = b:GetChildren()
x = s[math.random(#s)]
Report Abuse
yacker3000 is not online. yacker3000
Joined: 22 Nov 2008
Total Posts: 259
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
BunnyBoy26 is not online. BunnyBoy26
Joined: 17 Jun 2010
Total Posts: 5674
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image