Hello, I am trying to add a scoring module to my game. I’m also trying to save the highscore.
The current score module works just fine and updates every time to the current score but the highscore only gets updated once per run. next time I run it, it does not updates it and statys the same here is my code:
function scoreCode() --saving current score to the file local path local file path = system.pathForFile ( "score.txt", system.DocumentsDirectory) file = io.open (path, "w") file:write(score) io.close ( file ) local path = system.pathForFile ( "score.txt", system.DocumentsDirectory) local tempCurrentScore file = io.open ( path, "r" ) tempCurrentScore= file:read ("\*a") io.close ( file ) --Checking to see if new HighScore and saving new HighScore if system.pathForFile ( "highScore.txt", system.DocumentsDirectory) then local path = system.pathForFile ( "highScore.txt", system.DocumentsDirectory) local tempHighScore file = io.open ( path, "r" ) tempHighScore= file:read ("\*a") io.close ( file ) if tempCurrentScore \> tempHighScore then local path = system.pathForFile ( "highScore.txt", system.DocumentsDirectory) file = io.open ( path,"w") file:write(tempCurrentScore) io.close ( file ) print("YES IT CAME HERE CHECKINGGGGGGGGGGGGGG") else end else --HighScore dosent exists creating the file and updating highscore print("YES IT CAME HERE ELSEEEEEEEEEEEE") local path local file path = system.pathForFile ( "highScore.txt", system.DocumentsDirectory) file = io.open (path, "w") file:write(tempCurrentScore) io.close ( file ) print() end end scoreCode() composer.gotoScene( "gameOver" )
so basically what it does is simple write the current score to score.txt every time and then prints out in the gameOver scene that works fine. After saving the current score and closing the file it opens and reads it again and saves in tempCurrentScore and then checks to see if highscore.txt exists if it does it reads the highscore.txt and saves it in tempHighscore and then checks if tempCurrentScore>tempHighScore if it is it writes the tempCurrentScore to the file and closes it.
it works fine the 1st time but the complier does not comes to this piece of code the 2nd time. HELP!