How to select a random key from dictionary and then again

  1. What do you want to achieve?
    I want my script to select a random category by choosing a random dictionary and then select a random question which also is a dictionary and read all the values. Is that possible?

  2. What is the issue?
    image
    I can’t make it select random questions and then read the values of it.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I did and using # and HistoryQ[#] don’t work, the return 0 or nil.

 local categories={
"History", "Geography"
}

local HistoryQ = {
	["1"] = {"TEST1","fouransw","681","1681","765","2018","answer1"},
	["2"] = {"TEST2","fouransw","1018","1681","2020","856","answer1"}
}

local GeographyQ = {
	
	["1"] = {"TEST3","fouransw","681","1681","765","2018","answer1"},
	["2"] = {"TEST4","fouransw","1018","1681","2020","856","answer1"}

}

Don’t make the index a string, make it a number:

-- Do this
local t = {
    [1] = {}
}

-- Not this
local t = {
    ["1"] = {}
}

Then you can select a random index by doing:

local randomIndex = t[math.random(1, #t)]
2 Likes

That worked! Thank you so much!