Question

local questions={
    EN={
        Q1={"",""},
        Q2={"",""},
        Q3={"",""},
        Q4={"",""},
        Q5={"",""},
        Q6={"",""},
        Q7={"",""},
        Q8={"",""},
        Q9={"",""},
        Q10={"",""}
    },
    BR={
        Q1={"",""},
        Q2={"",""},
        Q3={"",""},
        Q4={"",""},
        Q5={"",""},
        Q6={"",""},
        Q7={"",""},
        Q8={"",""},
        Q9={"",""},
        Q10={"",""}
    },
    TR={
        Q1={"",""},
        Q2={"",""},
        Q3={"",""},
        Q4={"",""},
        Q5={"",""},
        Q6={"",""},
        Q7={"",""},
        Q8={"",""},
        Q9={"",""},
        Q10={"",""}
    }
}
local current_question = questions.EN.Q..math.random(1,10)[1]

I want to make the current_question variable return a random question from the questions table but it gives me an error; "attempt to index ? (a number value)"

Was it helpful?

Solution

You need to tell the interpreter Q..math.random(1,10) is one string like this:

local current_question = questions.EN["Q"..math.random(1,10)][1]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top