Save A Variable and load it (still havent figured it out):(

Hey guys I am going to post my code below I have been trying to have a money variable then when a button is touched it will save it and subtract the price variable and remeber what is left for future purchases in the game.

I don’t know what I am doing wrong? I tried many tutorials but none seem to work or I am not doing it right

   _             -----------------------------------------------------------------------------------------
                –
                – main.lua
                –
                -----------------------------------------------------------------------------------------
                json = require (‘json’)
                print(10-5)
                local widget = require (“widget”)
                print(numonscreen)
                local money = 10000
                local mypics = {“regularhouse.png”}

    – Save specified value to specified encrypted file
    function saveValue(loadsave, money)
     
    local theFile = loadsave
    local theValue = money
    local path = system.pathForFile( theFile, system.DocumentsDirectory )
     
    local file = io.open( path, “w+” )
    if file then – If the file exists, then continue. Another way to read this line is ‘if file == true then’.
    file:write(theValue) – This line will write the contents of the table to the .json file.
    io.close(file) – After we are done with the file, we close the file.
    return true – If everything was successful, then return true
    end
    end

                   – Load specified encrypted file, or create new file if it does not exist
    function loadValue(loadsave)
    local theFile = loadsave
    local path = system.pathForFile( theFile, system.DocumentsDirectory )
    local file = io.open( path, “r” )
     
    if file then – If file exists, continue. Another way to read this line is ‘if file == true then’.
    local contents = file:read( “*a” ) – read all contents of file into a string
    io.close( file ) – Since we are done with the file, close it.
    return contents – Return the table with the JSON contents
    else
    return ‘’ – Return nothing
    end
    end

                local rectangle = display.newRect(160,200,275,400)

                local buy = widget.newButton
                {
                    left = -45,
                    top = 350,
                    id = “buybtn”,
                    label = “Buy”,
                    
                }
                  

                local newcard = widget.newButton
                {

                    left = 160,
                    top = 350,
                    id = “newcardbtn”,
                    label = “New Card”,

                     

                }

                local onTouch
                local someNewText
                local centerX = display.contentCenterX
                local centerY = display.contentCenterY

                     
                local mph = display.newText(“Money Per Hour”,155,200,nil,35)  
                mph:setFillColor(1,0,0)
                local text = display.newText(math.random(1000,10000),centerX,centerY,nil,40)
                text:setFillColor(1,0,0)

                function someNewText()
                    text.text = math.random(1000,10000)
                    transition.to(text, {time = 100, alpha = 1})
                end
                function onTouch(event)
                    
                    if (event.phase == “began”) then
                                
                                print(“it began”)
                                transition.to(event.target,{time = 100, alpha = 0, onComplete = someNewText} )
                        local r = math.random ( 1, #mypics )local item = display.newImage ( mypics[r], 150, 150 )
                        local newcard1 = widget.newButton
                        {
                            left = 160,
                            top = 350,
                            id = “newcardbtn1”,
                            label = “New Card”,
                        }
                    
                        newcard1:addEventListener(“touch”,loadValue)
                        newcard1:addEventListener(“touch”,onTouch)
                    end
                end
                    text:addEventListener(“touch”,onTouch)

                function onTouch1( event )
                    if (event.phase == “began”) then
                        
                        print(money - text.text)
                        local money = display.newText(money - text.text,50,50,nil,36)
                        money:setFillColor(1,0,0)
                        money.trans = transition.to( money, { time = 2000, x = 0, alpha = 0 } )
                    end   
                end

                buy:addEventListener(“touch”,onTouch1)
                buy:addEventListener(“touch”,saveValue)
                
                local function touch2( event )
                    money3 = display.newText(money,50,50,nil,35)
                    money3:setFillColor(1,0,0)
                    if (event.phase == “ended”) then
                        money3:removeSelf()
                    end-- body
                end
                print(money)
              _

That’s a lot of code and you’ve posted it in italics (!?) and without using the code block. Please don’t do that, it just makes it hard to read. The code block button is right under the smiley face button in the text editor.

Your code problem, from the first line of your description, seems to be that you have a value which you need to store, subtract another value from it and then store the remainder. Is that correct?

To store values to a file you might want to simply keep a table of money values (as numbers or strings; doesn’t matter) and simply dump that whole table to a file using json - which I see you’re trying.

I’ve posted a utility library to the code exchange which does IO operations. It’s rather complete (ie: long) but each function is essentially self-container, so you can delete a lot that you don’t need: http://code.coronalabs.com/code/io-lib

The library doesn’t do encryption, but in Corona that is easy to implement.

If you can tidy up your code, or simply post a simpler version of what you’re doing, it would help. Do I have the premise correct and does my post help?

That’s a lot of code and you’ve posted it in italics (!?) and without using the code block. Please don’t do that, it just makes it hard to read. The code block button is right under the smiley face button in the text editor.

Your code problem, from the first line of your description, seems to be that you have a value which you need to store, subtract another value from it and then store the remainder. Is that correct?

To store values to a file you might want to simply keep a table of money values (as numbers or strings; doesn’t matter) and simply dump that whole table to a file using json - which I see you’re trying.

I’ve posted a utility library to the code exchange which does IO operations. It’s rather complete (ie: long) but each function is essentially self-container, so you can delete a lot that you don’t need: http://code.coronalabs.com/code/io-lib

The library doesn’t do encryption, but in Corona that is easy to implement.

If you can tidy up your code, or simply post a simpler version of what you’re doing, it would help. Do I have the premise correct and does my post help?