Question

I asked a question similar to this the other day, but another bug popped up "attempt to concatenate table and string"

local questions={
    EN={
        Q2={"Who is the lead developer of Transformice?","Tigrounette"},
        Q4={"What is Eminem's most viewed song on youtube?","I love the way you lie"},
        Q6={"What is the cubic root of 27?","3"},
        Q8={"What are the first 3 digits of Pi","3.141"},
        Q10={"What is the most populated country?","China"},
        Q12={"What is the plural of the word 'Person'?","People"},
        Q14={"Entomology is the science that studies ...?","Insects"},
        Q16={"Who was the creator of the (bot ran) minigame fight?","Cptp"},
        Q18={"The ozone layer restricts ... radiation","Ultraviolet"},
        Q20={"Filaria is caused by ...","Mosquitos"}
    }
local main = questions.EN["Q"..math.random(1,20)].."%s" -- bug here
local current_answer_en = string.gsub(current_question_en,1,2)
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top