Corona sdk connecting table to other lua file.Error bad argument#-1 newText

101 Views Asked by At

I'm having a problem regarding with my table. I can't connect the table to my other lua file. I have an error on local lblGiven = display.newText. It displays Error bad argument#-1 newText. The mechanic of this program is if you click the button a single part of the table will display as a label.

This is my file called questions.lua

local M = {

{
    "What is the answer 1", 
    answer = "17",

},
{
    "What is the answer 2", 
    answer = "18",

},
{   
    "What is the answer 3", 
    answer = "25",

},
},  

return M

This is my main.lua

local given = require("questions")

 local lblGiven = display.newText(
    {
        text = given[math.random(#given)],
        x = 160,
        y = 310,
        font = native.systemFont,
        align = "center"
    }
)
1

There are 1 best solutions below

0
On BEST ANSWER

Try

local M = {

{
    "What is the answer 1", 
    answer = "17",

},
{
    "What is the answer 2", 
    answer = "18",

},
{   
    "What is the answer 3", 
    answer = "25",

},
} -- remove comma

return M

and

local given = require("questions")

 local lblGiven = display.newText(
    {
        text = given[math.random(#given)][1],
        x = 160,
        y = 310,
        font = native.systemFont,
        align = "center"
    }
)

Instruction given[math.random(#given)] give you table. For example given[1] is equal {"What is the answer 1", answer = "17"}. To get only question you need use square parenthesis and index. Read more about tables from Understanding lua tables in corona sdk.