Well, I’ve done some extra digging and it seems that other users have had this issue as well in the past, for instance: https://forums.coronalabs.com/topic/59206-unable-to-save-score-between-sessions-error-could-not-read-nil/
I also made a new visual builds for win32 and Mac and asked friends to try them out. Those on a Mac had no issues, but everyone on Windows encountered the same issues as I’ve described. The first time that they run the project, everything works as expected, but every other start up it takes tens of attempts (official record is now 168 attempts at 100ms delay between attempts) until the save actually goes through.
The issue lies with io.open and seems to only affect Windows. It affects both running the code on simulator as well as building and running as a win32 build.
I would greatly appreciate it if anyone on Windows could confirm if they are also experiencing this issue.
Here is the updated code with visual indicators. I’ve also attached the project.
local json = require "json" local data = {1,2,3,4,5,6,7,8,9,10} local options = { text = "", x = display.contentCenterX, y = display.contentCenterY, width = display.actualContentWidth\*0.75, font = native.systemFont, fontSize = 18, align = "center" } local text = display.newText( options ) text:setFillColor(1,0,0) local tmr local function saveTable( dataTable, filename, count ) local myTable = json.encode( dataTable ) local path = system.pathForFile( filename, system.DocumentsDirectory ) local file, errorString = io.open( path, "w" ) if file then timer.cancel( tmr ) text:setFillColor(0,1,0) text.text = filename.." loaded\n\nAttempt #"..count file:write( myTable ) io.close( file ) else text.text = errorString.."\n\nAttempt #"..count end end local function loadTable( filename ) local path = system.pathForFile( filename, system.DocumentsDirectory ) local file = io.open( path, "w" ) if file then local contents = file:read( "\*a" ) local t = json.decode( contents ) io.close( file ) return t else print("Error when loading file") end end local path = system.pathForFile( "data.json", system.DocumentsDirectory ) local file = io.open( path, "r" ) -- if the file is loaded, then we run into issues if file then saveData = loadTable( "data.json" ) end local count = 1 local function rapidSave() saveTable( data, "data.json", count) count = count + 1 end tmr = timer.performWithDelay( 100, rapidSave, 0 )