[HELP] Problem with unlocking levels

Hi, Im using this for saving and loading :

 local saveValue = function( strFilename, strValue )  
 -- will save specified value to specified file  
 local theFile = strFilename  
 local theValue = strValue  
  
 local path = system.pathForFile( theFile, system.DocumentsDirectory )  
  
 -- io.open opens a file at path. returns nil if no file found  
 local file = io.open( path, "w+" )  
 if file then  
 -- write game score to the text file  
 file:write( theValue )  
 io.close( file )  
 end  
 end   
  
 local loadValue = function( strFilename )  
 -- will load specified file, or create new file if it doesn't exist  
  
 local theFile = strFilename  
  
 local path = system.pathForFile( theFile, system.DocumentsDirectory )  
  
 -- io.open opens a file at path. returns nil if no file found  
 local file = io.open( path, "r" )  
 if file then  
 -- read all contents of file into a string  
 local contents = file:read( "\*a" )  
 io.close( file )  
 return contents  
 else  
 -- create file b/c it doesn't exist yet  
 file = io.open( path, "w" )  
 file:write( "0" )  
 io.close( file )  
 return "0"  
 end  
 end  

Im using director class and in my main.lua I have this:

local loadunlock = function()  
\_G.levelunlock = loadValue( "levelunlock.data" );  
end  
loadunlock();  

When I complete level I unlock next level:

if \_G.levelunlock == 1 then  
 \_G.levelunlock = 2  
 saveValue("levelunlock.data", tonumber(\_G.levelunlock))  
end  

And then in level select:

if (tonumber(\_G.levelunlock) \>= 2) then  
-- Display unlocked image etc....  
end  

My problem is, that its works totally randomly :confused: . Sometimes I complete all my levels without problem and sometimes I stuck on 2nd level (only examle…it happens n other levels too…), because next level won’t open… Im totally lost, I dont understand why it happens…
Thanks for any help! [import]uid: 59968 topic_id: 14527 reply_id: 314527[/import]

Is the terminal printing any errors at any point? (NOT errors necessarily related to this, but any errors at all?)

That it is working sometimes and not other times makes me think you may have an error or two popping up that prevent it from saving properly in some instances. [import]uid: 52491 topic_id: 14527 reply_id: 53715[/import]

this code works:

[code]

local saveValue = function( strFilename, strValue )
– will save specified value to specified file
local theFile = strFilename
local theValue = strValue

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

– io.open opens a file at path. returns nil if no file found
local file = io.open( path, “w+” )
if file then
– write game score to the text file
file:write( theValue )
io.close( file )
end
end

local loadValue = function( strFilename )
– will load specified file, or create new file if it doesn’t exist

local theFile = strFilename

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

– io.open opens a file at path. returns nil if no file found
local file = io.open( path, “r” )
if file then
– read all contents of file into a string
local contents = file:read( “*a” )
io.close( file )
return contents
else
– create file b/c it doesn’t exist yet
file = io.open( path, “w” )
file:write( “0” )
io.close( file )
return “0”
end
end

local loadunlock = function()
_G.levelunlock = loadValue( “levelunlock.data” );
end
loadunlock();

local button = display.newRect(100, 100, 100, 100);

button:setFillColor(255, 0, 0)

function btn(e)

if (e.phase == “ended”) then

if _G.levelunlock == “0” then
_G.levelunlock = “1”;
saveValue(“levelunlock.data”, tonumber(_G.levelunlock));
end

if (tonumber(_G.levelunlock) == 1) then

button:setFillColor(0, 255, 0)

end

end

end

button:addEventListener(“touch”, btn); [import]uid: 63063 topic_id: 14527 reply_id: 53753[/import]