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

Im having a problem regarding with my table. I cant 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" } )

Each of your entries is another table, so you need to add [1] for the question or [2] for the answer when addressing given.

How? like this text = given[math.random([1]given)],?

Actually, you’ve currently got it like this:

given[1][1] = "what is the answer 1" given[1].answer = "17"

etc, etc.

So to make it consistent you can either do:

local M = { { question = "What is the answer 1", answer = "17", }, { question = "What is the answer 2", answer = "18", }, { question = "What is the answer 3", answer = "25", }, }, return M

 text = given[math.random(#given)].question text = given[math.random(#given)].answer

Or:

local M = { { "What is the answer 1", "17", }, { "What is the answer 2", "18", }, {  "What is the answer 3", "25", }, }, return M

text = given[math.random(#given)][1] text = given[math.random(#given)][2]

I have still have an error sir nick… it said bad argument #1 to ‘random’ (interval is empty). The error occur when i decided to make a table from another file…

Remove comma before return statement

local M = { { "What is the answer 1", "17", }, { "What is the answer 2", "18", }, {  "What is the answer 3", "25", }, } return M

Each of your entries is another table, so you need to add [1] for the question or [2] for the answer when addressing given.

How? like this text = given[math.random([1]given)],?

Actually, you’ve currently got it like this:

given[1][1] = "what is the answer 1" given[1].answer = "17"

etc, etc.

So to make it consistent you can either do:

local M = { { question = "What is the answer 1", answer = "17", }, { question = "What is the answer 2", answer = "18", }, { question = "What is the answer 3", answer = "25", }, }, return M

 text = given[math.random(#given)].question text = given[math.random(#given)].answer

Or:

local M = { { "What is the answer 1", "17", }, { "What is the answer 2", "18", }, {  "What is the answer 3", "25", }, }, return M

text = given[math.random(#given)][1] text = given[math.random(#given)][2]

I have still have an error sir nick… it said bad argument #1 to ‘random’ (interval is empty). The error occur when i decided to make a table from another file…

Remove comma before return statement

local M = { { "What is the answer 1", "17", }, { "What is the answer 2", "18", }, {  "What is the answer 3", "25", }, } return M