Cannot properly handle a JSON table

Hello guys, I’m using the LOADSAVE module to create and handle JSON tables for my game.

I have follow the topic that can be found @ https://forums.coronalabs.com/topic/34348-table-saving-problem/

in order to save the table only the first time the app is launched, and everything is working fine.

The problem arrives when I try to use it, for instance, the code below:

local myTable = loadsave.loadTable("myTable.json", system.DocumentsDirectory) if myTable==nil then local tV = {} tV.dubaiHasBeenPurchased = true tV.romaHasBeenPurchased = true tV.alaskaHasBeenPurchased = true tV.newYorkHasBeenPurchased = false tV.lasVegasHasBeenPurchased = false loadsave.saveTable(tV, "myTable.json", system.DocumentsDirectory) end if(tV.newYorkHasBeenPurchased==false)then envSetChoosed0 = 1 envSetChoosed1 = 2 envSetChoosed2 = 3 end if(tV.newYorkHasBeenPurchased==true and tV.lasVegasHasBeenPurchased==false)then envSetChoosed0 = 1 envSetChoosed1 = 2 envSetChoosed2 = 3 envSetChoosed3 = 4 end

returns “Attempt to index global ‘tV’ (a nil value)” on 

if(tV.newYorkHasBeenPurchased==false)then

What am I doing wrong?

Thank you.

  1. Move that local out of the if statement

    local tV = loadsave.loadTable(“myTable.json”, system.DocumentsDirectory) or { dubaiHasBeenPurchased = true, romaHasBeenPurchased = true, alaskaHasBeenPurchased = true, newYorkHasBeenPurchased = false, lasVegasHasBeenPurchased = false, } loadsave.saveTable(tV, “myTable.json”, system.DocumentsDirectory) if(tV.newYorkHasBeenPurchased==false) then envSetChoosed0 = 1 envSetChoosed1 = 2 envSetChoosed2 = 3 end if(tV.newYorkHasBeenPurchased==true and tV.lasVegasHasBeenPurchased==false)then envSetChoosed0 = 1 envSetChoosed1 = 2 envSetChoosed2 = 3 envSetChoosed3 = 4 end

  2. Why are you using two variables, myTable and tV?  Just use the one (like I did)

if myTable==nil then     local tV = {}     tV.dubaiHasBeenPurchased = true     tV.romaHasBeenPurchased = true     tV.alaskaHasBeenPurchased = true     tV.newYorkHasBeenPurchased = false     tV.lasVegasHasBeenPurchased = false     loadsave.saveTable(tV, "myTable.json", system.DocumentsDirectory) end

See how “tV” is local inside the if statement? It’s only alive inside the if statement. When you try to access it outside of the if, it doesn’t exist and you get the error you’re getting. 

I would recommend renaming tV to myTable since you already have it defined.

Rob

Hello guys, thank you for replying. 

 

I tried both @roaminggamer and @Rob Miracle suggestion, but none of them fixed my issue (I think I still doing something wrong).

 

So, trying to do what does @roaminggamer suggested, Corona returns

"game.lua:75: '}' expected (to close '{' at line 74) near '='" 

 where line 75 is

"tV.dubaiHasBeenPurchased = true" 

 and line 74 is 

"{"

My code now looks like 

local tV = loadsave.loadTable("myTable.json", system.DocumentsDirectory) or {     tV.dubaiHasBeenPurchased = true,     tV.romaHasBeenPurchased = true,     tV.alaskaHasBeenPurchased = true,     tV.newYorkHasBeenPurchased = false,     tV.lasVegasHasBeenPurchased = false,     tV.jungleHasBeenPurchased = false,     tV.hongKongHasBeenPurchased = false,     tV.underWaterHasBeenPurchased = false,     tV.universoHasBeenPurchased = false,     tV.singleCoinDoublerCounter = 0 } loadsave.saveTable(tV, "myTable.json", system.DocumentsDirectory)

While trying to do what does @Rob miracle suggested, Corona returns:

Attempt to compare number with nil

on

if(tV.singleCoinDoublerCounter \> 0)then

If I try to print “tV.singleCoinDoublerCounter” I get “nil”.

Following your suggestion, my code now looks like:

local tV = loadsave.loadTable("myTable.json", system.DocumentsDirectory) local tV = {} if tV==nil then     tV.dubaiHasBeenPurchased = true     tV.romaHasBeenPurchased = true     tV.alaskaHasBeenPurchased = true     tV.newYorkHasBeenPurchased = false     tV.lasVegasHasBeenPurchased = false     tV.jungleHasBeenPurchased = false     tV.hongKongHasBeenPurchased = false     tV.underWaterHasBeenPurchased = false     tV.universoHasBeenPurchased = false     tV.singleCoinDoublerCounter = 0 end loadsave.saveTable(tV, "myTable.json", system.DocumentsDirectory)

As you can see, I’ve added  tV.singleCoinDoublerCounter = 0 to my table

Sorry but I can’t get what am I doing wrong.

Thanks again

This part is wrong ( you replaced tV)

local tV = loadsave.loadTable("myTable.json", system.DocumentsDirectory) local tV = {} -- replaces last tV if tV==nil then

Try this change:

local tV = loadsave.loadTable("myTable.json", system.DocumentsDirectory) if tV==nil then tV = {} tV.dubaiHasBeenPurchased = true

Thank you so much!!! Now it is working fine!

Thanks again! :smiley:

  1. Move that local out of the if statement

    local tV = loadsave.loadTable(“myTable.json”, system.DocumentsDirectory) or { dubaiHasBeenPurchased = true, romaHasBeenPurchased = true, alaskaHasBeenPurchased = true, newYorkHasBeenPurchased = false, lasVegasHasBeenPurchased = false, } loadsave.saveTable(tV, “myTable.json”, system.DocumentsDirectory) if(tV.newYorkHasBeenPurchased==false) then envSetChoosed0 = 1 envSetChoosed1 = 2 envSetChoosed2 = 3 end if(tV.newYorkHasBeenPurchased==true and tV.lasVegasHasBeenPurchased==false)then envSetChoosed0 = 1 envSetChoosed1 = 2 envSetChoosed2 = 3 envSetChoosed3 = 4 end

  2. Why are you using two variables, myTable and tV?  Just use the one (like I did)

if myTable==nil then     local tV = {}     tV.dubaiHasBeenPurchased = true     tV.romaHasBeenPurchased = true     tV.alaskaHasBeenPurchased = true     tV.newYorkHasBeenPurchased = false     tV.lasVegasHasBeenPurchased = false     loadsave.saveTable(tV, "myTable.json", system.DocumentsDirectory) end

See how “tV” is local inside the if statement? It’s only alive inside the if statement. When you try to access it outside of the if, it doesn’t exist and you get the error you’re getting. 

I would recommend renaming tV to myTable since you already have it defined.

Rob

Hello guys, thank you for replying. 

 

I tried both @roaminggamer and @Rob Miracle suggestion, but none of them fixed my issue (I think I still doing something wrong).

 

So, trying to do what does @roaminggamer suggested, Corona returns

"game.lua:75: '}' expected (to close '{' at line 74) near '='" 

 where line 75 is

"tV.dubaiHasBeenPurchased = true" 

 and line 74 is 

"{"

My code now looks like 

local tV = loadsave.loadTable("myTable.json", system.DocumentsDirectory) or {     tV.dubaiHasBeenPurchased = true,     tV.romaHasBeenPurchased = true,     tV.alaskaHasBeenPurchased = true,     tV.newYorkHasBeenPurchased = false,     tV.lasVegasHasBeenPurchased = false,     tV.jungleHasBeenPurchased = false,     tV.hongKongHasBeenPurchased = false,     tV.underWaterHasBeenPurchased = false,     tV.universoHasBeenPurchased = false,     tV.singleCoinDoublerCounter = 0 } loadsave.saveTable(tV, "myTable.json", system.DocumentsDirectory)

While trying to do what does @Rob miracle suggested, Corona returns:

Attempt to compare number with nil

on

if(tV.singleCoinDoublerCounter \> 0)then

If I try to print “tV.singleCoinDoublerCounter” I get “nil”.

Following your suggestion, my code now looks like:

local tV = loadsave.loadTable("myTable.json", system.DocumentsDirectory) local tV = {} if tV==nil then     tV.dubaiHasBeenPurchased = true     tV.romaHasBeenPurchased = true     tV.alaskaHasBeenPurchased = true     tV.newYorkHasBeenPurchased = false     tV.lasVegasHasBeenPurchased = false     tV.jungleHasBeenPurchased = false     tV.hongKongHasBeenPurchased = false     tV.underWaterHasBeenPurchased = false     tV.universoHasBeenPurchased = false     tV.singleCoinDoublerCounter = 0 end loadsave.saveTable(tV, "myTable.json", system.DocumentsDirectory)

As you can see, I’ve added  tV.singleCoinDoublerCounter = 0 to my table

Sorry but I can’t get what am I doing wrong.

Thanks again

This part is wrong ( you replaced tV)

local tV = loadsave.loadTable("myTable.json", system.DocumentsDirectory) local tV = {} -- replaces last tV if tV==nil then

Try this change:

local tV = loadsave.loadTable("myTable.json", system.DocumentsDirectory) if tV==nil then tV = {} tV.dubaiHasBeenPurchased = true

Thank you so much!!! Now it is working fine!

Thanks again! :smiley: