What type of object is questions.EN["Q"..math.random(1,20)]
? Say random is 15, what type of object is questions.EN["Q6"]
? It is a {"What is the cubic root of 27?","3"}
, which is a table, which Lua doesn't know how to concatenate with a string ("%s"
in your case). If you want to concatenate with the first item of this table, then
local main = questions.EN["Q"..math.random(1,20)][1] .. "%s"
Note however that you will need the "random with step" function that I posted in math.random function with step option?, otherwise you could get that the table EN["Q"..something]
is nil (if the random number is an odd number, in the code you posted).
Note sure what you are trying to do with current_question_en
but if you are trying to extract the question and answer you could do something like this:
local QA = questions.EN["Q"..math.random(1,20)] -- this is a table
local question, answer = QA[1], QA[2]
The other option is that you build your table like this:
local questions={
EN={
Q2={q="Who is ..?", a="Tigrounette"},
Q4={q="What is ...?", a="I love the way you lie"},
...
}
}
Then you could use
local QA = questions.EN["Q"..math.random(1,20)] -- this is a table
print("The question:", QA.q)
print("The answer:", QA.a)
Not sure what you're trying to do with string.gsub
but it does not take integers as 2nd and 3rd args.