Problems Saving scores

Hi, I am using the score saving module from http://coronalabs.com/blog/2013/12/10/tutorial-howtosavescores/ and cannot get the file to save properly. While I am running the app it updates and changes perfectly, but when I test it on my phone, the file data resets every time I exit the app completely and re enter. Anyone know what I might be doing wrong(yes I used the score.save() ;)). Thanks in advance!

This is my high score updating system if you need to see. scoreSave is the name I used for the score module and score is the current score. 

[lua]if score > scoreSave.get() then

        scoreSave.set(score)

        scoreSave.save()

    end    [/lua]

This is happening to me too. I am on an Android. However, I am using a json file instead of a text file. Hopefully we can find a fix. I am using Rob’s score tutorial. 

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

OK Thanks ill check it out!

THANKS. It works!!!

Does the code work on your android? I used it practically line for line but I am having major issues.

No but the Robs score works and I like that one better anyways :slight_smile:

Robs score tutorial works ios android for me just to clear things up

Here is my code. The first time my app opens on a device a new file should be created with blank scores. However, the scores won’t save and the app makes a new blanks save file every time app opens.

local function continue() storyboard.gotoScene( "title" ) end function loadTableAgain(filename) path = system.pathForFile( filename, system.DocumentsDirectory) contents = "" gameData = {} file = io.open( path, "r" ) if file then print("Loaded again successfully") -- read all contents of file into a string contents = file:read( "\*a" ) gameData = json.decode(contents); io.close( file ) \_G.s1 = gameData.s1 \_G.s2 = gameData.s2 \_G.s3 = gameData.s3 \_G.s4 = gameData.s4 \_G.s5 = gameData.s5 \_G.coins = gameData.coins timer.performWithDelay(1500,continue) return gameData end return nil end local json = require("json") function saveTable(t, filename) path = system.pathForFile( filename, system.DocumentsDirectory) file = io.open(path, "w") if file then contents = json.encode(t) file:write( contents ) io.close( file ) print("Could Save to- "..path.."\nFile- "..filename) gameData = loadTableAgain("mygamesettings.json") return true else print("Couldn't Save") return false end end local function saveG() myGameSettings = {} myGameSettings.s1 = 0 myGameSettings.s2 = 0 myGameSettings.s3 = 0 myGameSettings.s4 = 0 myGameSettings.s5 = 0 myGameSettings.coins = 0 saveTable(myGameSettings, "mygamesettings.json") end function loadTable(filename) path = system.pathForFile( filename, system.DocumentsDirectory) print(path) contents = "" gameData = {} file = io.open( path, "r " ) if file then -- read all contents of file into a string contents = file:read( "\*a" ) gameData = json.decode(contents); io.close( file ) \_G.s1 = gameData.s1 \_G.s2 = gameData.s2 \_G.s3 = gameData.s3 \_G.s4 = gameData.s4 \_G.s5 = gameData.s5 \_G.coins = gameData.coins print("could load"..path.."\nFile- "..filename) timer.performWithDelay(1500,continue) return gameData else print("couldn't load") saveG() end return nil end local function loadG() gameData = loadTable("mygamesettings.json") end loadG()

Umm, sorry I am still kind of new to Corona. This seems really complicated in my opinion. I just use the two functions given but from my experience I just do something like this if it helps.

I just load the file and if it is empty it create the defaults for the variables. If this doesn’t help you just let me know and I can see what I can do :slight_smile:

[lua]

local json = require(“json”)

– Function to save a table.  Since game settings need to be saved from session to session, we will

– use the Documents Directory

local json = require(“json”)

function saveTable(t, filename)

    local path = system.pathForFile( filename, system.DocumentsDirectory)

    local file = io.open(path, “w”)

    if file then

        local contents = json.encode(t)

        file:write( contents )

        io.close( file )

        return true

    else

        return false

    end

end

function loadTable(filename)

    local path = system.pathForFile( filename, system.DocumentsDirectory)

    local contents = “”

    local myTable = {}

    local file = io.open( path, “r” )

    if file then

         – read all contents of file into a string

         local contents = file:read( “*a” )

         myTable = json.decode(contents);

         io.close( file )

         return myTable 

    end

    return nil

end

local score = 0

local gameData = loadTable(“gameData.json”)

if gameData == nil then

    gameData = {}

    gameData.highscore = 0

end 

highscore = gameData.highscore

[/lua]

Never mind. I looked at the docs and found out what to add. Thanks anyway

This is happening to me too. I am on an Android. However, I am using a json file instead of a text file. Hopefully we can find a fix. I am using Rob’s score tutorial. 

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

OK Thanks ill check it out!

THANKS. It works!!!

Does the code work on your android? I used it practically line for line but I am having major issues.

No but the Robs score works and I like that one better anyways :slight_smile:

Robs score tutorial works ios android for me just to clear things up

Here is my code. The first time my app opens on a device a new file should be created with blank scores. However, the scores won’t save and the app makes a new blanks save file every time app opens.

local function continue() storyboard.gotoScene( "title" ) end function loadTableAgain(filename) path = system.pathForFile( filename, system.DocumentsDirectory) contents = "" gameData = {} file = io.open( path, "r" ) if file then print("Loaded again successfully") -- read all contents of file into a string contents = file:read( "\*a" ) gameData = json.decode(contents); io.close( file ) \_G.s1 = gameData.s1 \_G.s2 = gameData.s2 \_G.s3 = gameData.s3 \_G.s4 = gameData.s4 \_G.s5 = gameData.s5 \_G.coins = gameData.coins timer.performWithDelay(1500,continue) return gameData end return nil end local json = require("json") function saveTable(t, filename) path = system.pathForFile( filename, system.DocumentsDirectory) file = io.open(path, "w") if file then contents = json.encode(t) file:write( contents ) io.close( file ) print("Could Save to- "..path.."\nFile- "..filename) gameData = loadTableAgain("mygamesettings.json") return true else print("Couldn't Save") return false end end local function saveG() myGameSettings = {} myGameSettings.s1 = 0 myGameSettings.s2 = 0 myGameSettings.s3 = 0 myGameSettings.s4 = 0 myGameSettings.s5 = 0 myGameSettings.coins = 0 saveTable(myGameSettings, "mygamesettings.json") end function loadTable(filename) path = system.pathForFile( filename, system.DocumentsDirectory) print(path) contents = "" gameData = {} file = io.open( path, "r " ) if file then -- read all contents of file into a string contents = file:read( "\*a" ) gameData = json.decode(contents); io.close( file ) \_G.s1 = gameData.s1 \_G.s2 = gameData.s2 \_G.s3 = gameData.s3 \_G.s4 = gameData.s4 \_G.s5 = gameData.s5 \_G.coins = gameData.coins print("could load"..path.."\nFile- "..filename) timer.performWithDelay(1500,continue) return gameData else print("couldn't load") saveG() end return nil end local function loadG() gameData = loadTable("mygamesettings.json") end loadG()

Umm, sorry I am still kind of new to Corona. This seems really complicated in my opinion. I just use the two functions given but from my experience I just do something like this if it helps.

I just load the file and if it is empty it create the defaults for the variables. If this doesn’t help you just let me know and I can see what I can do :slight_smile:

[lua]

local json = require(“json”)

– Function to save a table.  Since game settings need to be saved from session to session, we will

– use the Documents Directory

local json = require(“json”)

function saveTable(t, filename)

    local path = system.pathForFile( filename, system.DocumentsDirectory)

    local file = io.open(path, “w”)

    if file then

        local contents = json.encode(t)

        file:write( contents )

        io.close( file )

        return true

    else

        return false

    end

end

function loadTable(filename)

    local path = system.pathForFile( filename, system.DocumentsDirectory)

    local contents = “”

    local myTable = {}

    local file = io.open( path, “r” )

    if file then

         – read all contents of file into a string

         local contents = file:read( “*a” )

         myTable = json.decode(contents);

         io.close( file )

         return myTable 

    end

    return nil

end

local score = 0

local gameData = loadTable(“gameData.json”)

if gameData == nil then

    gameData = {}

    gameData.highscore = 0

end 

highscore = gameData.highscore

[/lua]

Never mind. I looked at the docs and found out what to add. Thanks anyway