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
|
  |
| 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
|
  |
| Joined: 21 Jan 2012 |
| Total Posts: 5667 |
|
|
| 13 Dec 2016 06:52 PM |
@Wrath does not work in this case
New_Item |
|
|
| Report Abuse |
|
|
|
| 13 Dec 2016 06:54 PM |
local RandomQuestion = _G.TriviaQuestions[1,#_G.TriviaQuestions].MainQuestion
|
|
|
| Report Abuse |
|
|
|
| 13 Dec 2016 06:55 PM |
Add math.random to that sorry ^
|
|
|
| Report Abuse |
|
|
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 |
|
|
|
| 13 Dec 2016 06:59 PM |
So it looks like this?
local RandomQuestion = _G.TriviaQuestions[math.random(1,#_G.TriviaQuestions)].MainQuestion
|
|
|
| Report Abuse |
|
|
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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| Joined: 21 Jan 2012 |
| Total Posts: 5667 |
|
|
| 13 Dec 2016 07:13 PM |
_G is great for tables and such
New_Item |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
| |
|
|
| 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
|
  |
| Joined: 21 Jan 2012 |
| Total Posts: 5667 |
|
| |
|
|
| 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 |
|
|