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 » Scripters
Home Search
 

Re: How can I get values off a table using math.random

Previous Thread :: Next Thread 
killerbot29003 is not online. killerbot29003
Joined: 04 Oct 2014
Total Posts: 3054
31 Jan 2016 08:59 AM
For example:
math.randomseed(math.floor(os.time()))
T = {4,2,3,4,5,6,7,4,8,5,2,4,2,7,3,7}

How would I use math.random and get a value off that table?




This siggy is copyrighted ©



Report Abuse
128Gigabytes is online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
31 Jan 2016 09:01 AM
print(t[math.random(1, #t)])

Will error if t is empty.
Report Abuse
Aethex is not online. Aethex
Joined: 16 Oct 2011
Total Posts: 2193
31 Jan 2016 10:18 AM
@128Gigabytes Should be: print(t[math.random(1, #T)])

His variable is T, not t.
Report Abuse
XYuy is not online. XYuy
Joined: 24 Dec 2010
Total Posts: 5628
31 Jan 2016 10:27 AM
@Aeth

That doesn't matter.

He gave the right example, and anyone who knows how to script knows how to replace variables.

Report Abuse
Gensoki is not online. Gensoki
Joined: 15 Feb 2014
Total Posts: 1982
31 Jan 2016 10:30 AM
Yet it should be
print(T[math.random(1, #T)])
not t :)


http://www.roblox.com/Schwarzesmarken-Opening-TV-Size-item?id=345656154 #code "Let's just say goodbye"
Report Abuse
Aethex is not online. Aethex
Joined: 16 Oct 2011
Total Posts: 2193
31 Jan 2016 10:34 AM
@XYuy It does matter. The level of op is not known and code provided should work for the given example. It was expressed as code that works and should do so.
Report Abuse
Aethex is not online. Aethex
Joined: 16 Oct 2011
Total Posts: 2193
31 Jan 2016 10:35 AM
I also made a mistake, though, as Gensoki pointed out. I only changed the second t: print(T[math.random(1, #T)])
Report Abuse
128Gigabytes is online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
31 Jan 2016 02:29 PM
While we are at it this will stop it from erroring if the table is empty

print(T[(#T > 0) and math.random(1, #T) or 0])
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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