Saving level /settings/ high score in between force quits

See that’s where my problem is. The user, (in this case me) plays a session, and makes a high score of 100 and  loses (score’s already saved and no more updates) and  goes to menu screen. By now the scores are already saved. However,  if he exits and force quits now, that 100 high score is gone and it’s back to 0.

So I am not sure what’s going wrong. Would it be because the app is in suspended mode and since it’s being force quit the OS gets rid of the system.Documents files? But then again if I go to app data on settings, I see that the temp and documents files are there and occupying memory.

Can you post the code where you load and save the score?

I actually tried your code before and it worked for me fine in the simulator:

score.lua

local M = {} -- Create the local module table (this will hold our functions and data) M.score = 0 M.filename = "score.txt" function M.set( value ) M.score = value M.scoreText = string.format( "% 6d", M.score ) end function M.get() return M.score end function M.add( amount ) M.score = M.score + amount M.scoreText = string.format( "% 6d" , M.score ) end function M.save() local path = system.pathForFile( M.filename, system.DocumentsDirectory ) local file = io.open(path, "w") if ( file ) then local contents = tostring( M.score ) file:write( contents ) io.close( file ) return true else print( "Error: could not read ", M.filename, "." ) return false end end function M.load() local path = system.pathForFile( M.filename, system.DocumentsDirectory ) local contents = "" local file = io.open( path, "r" ) if ( file ) then -- Read all contents of file into a string local contents = file:read( "\*a" ) local score = tonumber(contents); io.close( file ) return score else print( "Error: could not read scores from ", M.filename, "." ) end return nil end return M

main.lua

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local score = require("score") local function saveScore() score.set( 2 ) score.save() end local function loadScore() local s = score.load() score.set( s ) print("Current score: ", score.get() ) end -- Call this on your first try to set and save the score: -- saveScore() -- Call this on your second try to load the score: -- loadScore()

Ahhhh…I think I see the problem, it wasn’t the score.lua rather the saving in game. I use set and save every time. But I never load the score. So if it got force quit, I was using get instead of load, which was returning me a 0, since force quitting set everything back to 0. I wasn’t using load, so I never got the saved score returned. 

Thank you very much Tomas for your help. Really appreciate it :smiley: