problem with score

I’ve read this post https://coronalabs.com/blog/2013/12/10/tutorial-howtosavescores/ but unluckily I can’t make it works, the score can’t be saved or loaded, txt file is always empty. When I play the app on my phone it’s the same. Then I tried this code from QuizMaster in this topic https://forums.coronalabs.com/topic/58147-help-creating-a-score-counter-and-saving-highscore/ and same result.

So my problems are:

  1. the high score can’t be saved/loaded from txt file 

  2. When I replaythe game, set score to 0, then get the score again to display in gameover scene, but the score is always equal to high score…  :wacko:

here is the code in gameover scene:

local highScore \_path = system.pathForFile( "highScore.txt", system.DocumentsDirectory ) \_file = io.open( \_path, "r" ) for line in \_file:lines() do highScore = tonumber (line) end io.close( \_file ) \_file = nil \_path = nil local currentScore=score.get() if currentScore \> highScore then highScore = currentScore \_path = system.pathForFile( "highScore.txt", system.DocumentsDirectory ) \_file = io.open( \_path, "w" ) \_file:write( highScore ) io.close( \_file ) \_file = nil \_path = nil end local function toGamescr() local gsound = audio.loadSound("btnsound.mp3") local psound = audio.play(gsound) composer.gotoScene("restart","crossFade", 300) return true end local function toGamescr0() local gsound = audio.loadSound("btnsound.mp3") local psound = audio.play(gsound) score.set(0) composer.gotoScene("restart","crossFade", 300) return true end

restart scene only has function to check which stage to restart to.

in game scene, whenever the score is added, I saved it. I used one of the two methods I mentioned above but it didn’t work so now I tried both of them, have the scorefile.txt and highScore.txt, I want to compare the score from both of them but nothing works… I’m totally confused, please help me about this.

First of all, are you getting any errors in your console?

Secondly, have you placed any print statements in your code (they show in your console) to see what your values are?

It looks like you’re using global variables

Thank you for your reply, I don’t get any errors in the console, it only shows me the text “can’t load” to indicate that it doesn’t work, when I play this app in my phone, this text doesn’t show, but when I exit and start the app again the score doesn’t show (it’s not saved or it can’t load?)

In the code above, I tried to compared the score.get() with highscore from an empty file, highscore always equal to score.get(), and the value doesn’t change (it’s not affected by the updated score anymore, for example 1st time the score is 400, next try is 900 but in the gameover scene it’s always 400)

In game scene, the score updates smoothly.

I tried to test the new code in gameover scene, instead of using score.get() I used score.load(), the result is nil but I had the score.save() function whenever I add the score… I think the score is not saved properly but I don’t see any warning text in the console. Txt file is always empty.

When I removed all the highScore code and replaced with score.load(), it didn’t work and again, show error about nil value…

Now I’m really confused what did I do wrong?

I edit the code a little, removed function to check if file exist, then tried it on my phone. I got this result… 

78c24909-4e7c-4d5a-bc41-34b62faa0715_zps

score.lua line 105:

local contents = file:read( “*a” )

so in my phone it really can’t read or store file… file doesn’t exist. What should I do so it can recognize the file? 

UPDATE:

I got it work, now it can load and read the file. The remaining problem is the current score/highscore in gameover scene not updated. It only load and save one time only…

UPDATE2:

ok I got everything work :smiley: problem solved.

First of all, are you getting any errors in your console?

Secondly, have you placed any print statements in your code (they show in your console) to see what your values are?

It looks like you’re using global variables

Thank you for your reply, I don’t get any errors in the console, it only shows me the text “can’t load” to indicate that it doesn’t work, when I play this app in my phone, this text doesn’t show, but when I exit and start the app again the score doesn’t show (it’s not saved or it can’t load?)

In the code above, I tried to compared the score.get() with highscore from an empty file, highscore always equal to score.get(), and the value doesn’t change (it’s not affected by the updated score anymore, for example 1st time the score is 400, next try is 900 but in the gameover scene it’s always 400)

In game scene, the score updates smoothly.

I tried to test the new code in gameover scene, instead of using score.get() I used score.load(), the result is nil but I had the score.save() function whenever I add the score… I think the score is not saved properly but I don’t see any warning text in the console. Txt file is always empty.

When I removed all the highScore code and replaced with score.load(), it didn’t work and again, show error about nil value…

Now I’m really confused what did I do wrong?

I edit the code a little, removed function to check if file exist, then tried it on my phone. I got this result… 

78c24909-4e7c-4d5a-bc41-34b62faa0715_zps

score.lua line 105:

local contents = file:read( “*a” )

so in my phone it really can’t read or store file… file doesn’t exist. What should I do so it can recognize the file? 

UPDATE:

I got it work, now it can load and read the file. The remaining problem is the current score/highscore in gameover scene not updated. It only load and save one time only…

UPDATE2:

ok I got everything work :smiley: problem solved.