Problem with compare variable from another file.

Hi I’m geting an error “attempt to indext field ‘?’ (a nil value)” when I’m trying to compare a variable from another lua file.

What I’m I doing wrong here? it’s working if I set “i = 1” instead of “i = myData.settings.currentLevel”  one thought was that “myData.settings.levels[i].score” and “myData.settings.currentLevel” is read from mydata.lua at the same time. So  the “i” in “myData.settings.levels[i].score” never change?

thanks in advance :slight_smile:

--mydata.lua-- local M = {} M.maxLevels = 16 M.settings = {} M.settings.currentLevel = 1 M.settings.levels = {} M.settings.levels[1] = {} M.settings.levels[1].stars = 1 M.settings.levels[1].score = 1 return M 

--game.lua-- local myData = require( "mydata" ) i = myData.settings.currentLevel if myData.settings.levels[i].score \> 1 then print("yes") end

Hello snobban,

Did you find a solution for your problem?
Right now I’m having the same problem.

regards,

Guy

Use print statements to go “down the hierarchy” to see why you get a nil value this way.

print(myData)

print(myData.settings)

print(myData.settings.levels)

print(myData.settings.levels[1])

print(myData.settings.levels[1].score)

See at what point you get nil and let us know.

Hello yes, Guy I found a solution forgot to poste here :slight_smile:

Apparently I hade to add “tonumber” to convert from string to number.
Read more about tonumber here https://docs.coronalabs.com/api/library/global/tonumber.html

Good luck coding and I hope I could be of help 

Regards Snobban :slight_smile:

Thank you Snobban and Thomas6.

“tonumber”  did the work, problem solved

Regards,

Guy

Hi - I saw it yesterday; good for you and thanks for notifying. Tricky one, I would have imagined Lua casting it to integer.

Hello snobban,

Did you find a solution for your problem?
Right now I’m having the same problem.

regards,

Guy

Use print statements to go “down the hierarchy” to see why you get a nil value this way.

print(myData)

print(myData.settings)

print(myData.settings.levels)

print(myData.settings.levels[1])

print(myData.settings.levels[1].score)

See at what point you get nil and let us know.

Hello yes, Guy I found a solution forgot to poste here :slight_smile:

Apparently I hade to add “tonumber” to convert from string to number.
Read more about tonumber here https://docs.coronalabs.com/api/library/global/tonumber.html

Good luck coding and I hope I could be of help 

Regards Snobban :slight_smile:

Thank you Snobban and Thomas6.

“tonumber”  did the work, problem solved

Regards,

Guy

Hi - I saw it yesterday; good for you and thanks for notifying. Tricky one, I would have imagined Lua casting it to integer.