how to save high score

hey guys  sorry for being a bug but i am having trouble saving my high score.  my high score like this

highscore = 0 score =0 local function save( event ) -- function to save score local path = system.pathForFile( "score.txt", system.DocumentsDirectory ) -- where the file will be saved local file = io.open( path, "w+b" ) -- opening the file file:write(highscore.."") -- writing the variable 'score' in the score.txt file io.close( file ) -- closing the file -- Saves our data end local function resumeStart() local path = system.pathForFile( "score.txt", system.DocumentsDirectory ) -- where the file will be saved local file = io.open( path, "r" ) -- opens the file under the variable file if file then -- if there is a file then local contents = file:read( "\*a" ) -- read the contents of the file, and read the whole string(\*a) local prevState = explode(", ", contents) -- put the contents of score.txt into a table print('file') highscore = prevState[1] -- read the table at position 1 and assign the variable score io.close( file ) -- close file else -- if there is no file highscore=0 -- the score then starts at 0 end end --ANSCA sample code-- function explode(div,str) if (div=='') then return false end local pos,arr = 0,{} -- for each divider found for st,sp in function() return string.find(str,div,pos,true) end do table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider pos = sp + 1 -- Jump past current divider end table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider return arr end --ANSCA sample code^-- resumeStart() -- call the starting function function scoreUpdate(event) score = score +100 scoreText.text = ""..score ---scoreText:setTextColor(225,225,225) --scoreText:setReferencePoint(display.TopLeftReferencePoint) scoreText.x = 150 scoreText.y = 40 if highscore \< score then print "highscore" highscore = highscore +100 highscoreText.text = ""..highscore --scores:increment( "player2") --scores:save() save() end end

the problem is i want to compare two value  the high score and score it self like this

if highscore \< score then print "highscore" highscore = highscore +100 highscoreText.text = ""..highscore save() end &nbsp;

 but i keep getting this error  “attempt to compare string with number” 

also could  i just use Apple or google leaderboard system to save my high scores? i am very lost on this please help. 

i even try using ice box  but no luck :frowning:

thanks 

from Sean :slight_smile:

Your post is nearly impossible to read.  Can you please use the code tags located in the toolbar, they look like this <>

Our library may help you - https://github.com/GlitchGames/GGScore

i fix the code could someone help please 

Your code still looks hard to read, I didn’t even bother. I suggest you use GGScore from the guys at Glitch Games or the ICE library file, they are far simpler to use than what you are trying to do yourself

okay but how do i use GGscores because i don’t understand it. ice box make my game  lag so i don’t use it thanks :) 

 function save( event ) -- function to save score local path = system.pathForFile( "score.txt", system.DocumentsDirectory ) -- where the file will be saved local file = io.open( path, "w+b" ) -- opening the file file:write(score01 .."") -- writing the variable 'score' in the score.txt file io.close( file ) -- closing the file -- Saves our data end function resumeStart(event) local path = system.pathForFile( "score.txt", system.DocumentsDirectory ) -- where the file will be saved local file = io.open( path, "r" ) -- opens the file under the variable file if file then -- if there is a file then local contents = file:read( "\*a" ) -- read the contents of the file, and read the whole string(\*a) local prevState = explode(", ", contents) -- put the contents of score.txt into a table -- print('file') score01 = prevState[1] -- read the table at position 1 and assign the variable score io.close( file ) -- close file else -- if there is no file score01=0 -- the score then starts at 0 end end --ANSCA sample code-- function explode(div,str) if (div=='') then return false end local pos,arr = 0,{} -- for each divider found for st,sp in function() return string.find(str,div,pos,true) end do table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider pos = sp + 1 -- Jump past current divider end table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider return arr end --ANSCA sample code^ resumeStart() -- call the starting function function scoretapped() -- function called when user presses the add button score01=score01+100 -- increases the score by 50 print "highscore" save() -- calls save, so when the user reloads the application the score is still there t.text=score -- updates t to reflect new score end 

 is this better :slight_smile:

i just want to save high score with no problem 

Yes it is, but I would still advise using one of the libraries I mentioned above. They do all this for you and there is some examples of implementation. It makes life a lot easier

It’s slightly more readable however proper indentation goes a very long way.

If you didn’t like Ice then you might prefer GGData, it’s our updated and much improved version.

I use ice myself

my problem is how would i use gg data or even ice box  to save this 

 function scoreUpdate(event)

 score = score +100 

 scoreText.text = “”…score

—scoreText:setTextColor(225,225,225)

 --scoreText:setReferencePoint(display.TopLeftReferencePoint)

 scoreText.x = 150

 scoreText.y = 40

if highscore < score then 

 print “highscore”

highscore = highscore + 100 

highscoreText.text = “”…highscore 

  

end 

end

i am having a real hard time trying to save the high score in my games 

I have replied to your PM, hopefully that helps.

Your post is nearly impossible to read.  Can you please use the code tags located in the toolbar, they look like this <>

Our library may help you - https://github.com/GlitchGames/GGScore

i fix the code could someone help please 

Your code still looks hard to read, I didn’t even bother. I suggest you use GGScore from the guys at Glitch Games or the ICE library file, they are far simpler to use than what you are trying to do yourself

okay but how do i use GGscores because i don’t understand it. ice box make my game  lag so i don’t use it thanks :) 

 function save( event ) -- function to save score local path = system.pathForFile( "score.txt", system.DocumentsDirectory ) -- where the file will be saved local file = io.open( path, "w+b" ) -- opening the file file:write(score01 .."") -- writing the variable 'score' in the score.txt file io.close( file ) -- closing the file -- Saves our data end function resumeStart(event) local path = system.pathForFile( "score.txt", system.DocumentsDirectory ) -- where the file will be saved local file = io.open( path, "r" ) -- opens the file under the variable file if file then -- if there is a file then local contents = file:read( "\*a" ) -- read the contents of the file, and read the whole string(\*a) local prevState = explode(", ", contents) -- put the contents of score.txt into a table -- print('file') score01 = prevState[1] -- read the table at position 1 and assign the variable score io.close( file ) -- close file else -- if there is no file score01=0 -- the score then starts at 0 end end --ANSCA sample code-- function explode(div,str) if (div=='') then return false end local pos,arr = 0,{} -- for each divider found for st,sp in function() return string.find(str,div,pos,true) end do table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider pos = sp + 1 -- Jump past current divider end table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider return arr end --ANSCA sample code^ resumeStart() -- call the starting function function scoretapped() -- function called when user presses the add button score01=score01+100 -- increases the score by 50 print "highscore" save() -- calls save, so when the user reloads the application the score is still there t.text=score -- updates t to reflect new score end 

 is this better :slight_smile:

i just want to save high score with no problem