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
 

Random Table Value?

Previous Thread :: Next Thread 
New_Item is not online. New_Item
Joined: 21 Jan 2012
Total Posts: 5667
13 Dec 2016 06:43 PM
How do I get a completely random table value with this (I want to get a random "Question")?

_G.TriviaQuestions = {
Question = {
MainQuestion = "What color is Rudolph's nose?",
CorrectAnswer = "Red",
FalseAnswer1 = "Blue",
FalseAnswer2 = "Green",
FalseAnswer3 = "Brown",
},
Question = {
MainQuestion = "What does Santa fly?",
CorrectAnswer = "Sleigh",
FalseAnswer1 = "Deer",
FalseAnswer2 = "Plane",
FalseAnswer3 = "Magic Carpet",
},
}





New_Item
Report Abuse
Wrathsong is online. Wrathsong
Joined: 05 Jul 2012
Total Posts: 22393
13 Dec 2016 06:47 PM
tab[math.random(1, #tab)]


Ever wanted to learn how to script? Check out my YouTube channel: youtube.com/austintheslayer Want to see me code in action? Follow my twitch: twitch.tv/austinrblx
Report Abuse
New_Item is not online. New_Item
Joined: 21 Jan 2012
Total Posts: 5667
13 Dec 2016 06:52 PM
@Wrath does not work in this case


New_Item
Report Abuse
FlamedSkull is not online. FlamedSkull
Joined: 23 May 2009
Total Posts: 1379
13 Dec 2016 06:54 PM
local RandomQuestion = _G.TriviaQuestions[1,#_G.TriviaQuestions].MainQuestion


Report Abuse
FlamedSkull is not online. FlamedSkull
Joined: 23 May 2009
Total Posts: 1379
13 Dec 2016 06:55 PM
Add math.random to that sorry ^


Report Abuse
New_Item is not online. New_Item
Joined: 21 Jan 2012
Total Posts: 5667
13 Dec 2016 06:57 PM
@Flame I always get this error

'17:56:23.881 - ServerScriptService.Script:18: bad argument #2 to 'random' (interval is empty)'


New_Item
Report Abuse
FlamedSkull is not online. FlamedSkull
Joined: 23 May 2009
Total Posts: 1379
13 Dec 2016 06:59 PM
So it looks like this?

local RandomQuestion = _G.TriviaQuestions[math.random(1,#_G.TriviaQuestions)].MainQuestion


Report Abuse
Wrathsong is online. Wrathsong
Joined: 05 Jul 2012
Total Posts: 22393
13 Dec 2016 07:07 PM
ah prob because # of a dictionary will print 0 or nil or something


Ever wanted to learn how to script? Check out my YouTube channel: youtube.com/austintheslayer Want to see me code in action? Follow my twitch: twitch.tv/austinrblx
Report Abuse
New_Item is not online. New_Item
Joined: 21 Jan 2012
Total Posts: 5667
13 Dec 2016 07:09 PM
Yeah my script looks like that but it still prints the error.


New_Item
Report Abuse
nox7 is not online. nox7
Joined: 29 Aug 2008
Total Posts: 27467
13 Dec 2016 07:09 PM
The issue here is that you have defined custom indexes. You cannot get the length of the table and using numerical indexes (like table[number]) will result in a nil value.

I suggest generating a new table on the fly of questions, then selecting from it.

function getRandomQuestion()
local temp = {}
for index, question in pairs(_G.TriviaQuestions) do
table.insert(temp, question)
end
return temp[math.random(1, #temp)]
end

local randomQuestion = getRandomQuestion()
Report Abuse
Real_Spooky is not online. Real_Spooky
Joined: 23 Oct 2013
Total Posts: 3149
13 Dec 2016 07:09 PM
Stop using _G

The global table is essentially deprecated and worthless in the ROBLOX-Lua API
Report Abuse
New_Item is not online. New_Item
Joined: 21 Jan 2012
Total Posts: 5667
13 Dec 2016 07:13 PM
_G is great for tables and such


New_Item
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
13 Dec 2016 07:15 PM
Well your table won't even work how you want it to, the second question replaces the first.
Report Abuse
Real_Spooky is not online. Real_Spooky
Joined: 23 Oct 2013
Total Posts: 3149
13 Dec 2016 07:18 PM
Do you even know what _G does?

It's the global namespace.

Meaning using '_G.[this]' is EXACTLY EQUIVALENT TO just '[this]'.
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
13 Dec 2016 07:19 PM
Not in ROBLOX it's not.
Report Abuse
Real_Spooky is not online. Real_Spooky
Joined: 23 Oct 2013
Total Posts: 3149
13 Dec 2016 07:19 PM
Yeah, I know. It shares between scripts. But it's still crappy practice considering we now have:

-BindableEvents
-ModuleScripts
-RemoteEvents
Report Abuse
New_Item is not online. New_Item
Joined: 21 Jan 2012
Total Posts: 5667
13 Dec 2016 07:20 PM
@128 ur right


New_Item
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
13 Dec 2016 07:23 PM
You should just use an array and do it the way Wrathsong said to do it, if you were going to name all your keys the same thing clearly you didn't need keys.
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