how to implement high score

ok so I’ve implemented a high score from the high score tutorial which uses the score.lua file , but i need to use it for a fruit ninja style game so i want it to tell the user there score as there playing and then when they hit a bomb it shows a replay screen where i want there highscore to show , but when they press replay and the game starts again , the score follows on from there last score , so if you have 4 consecutive goes it will keep carrying the score over rather than starting the current score again and saving the high score

this is the code i have used

local score = require( "score" ) function loadScore( event ) if event.phase == "ended" then local prevScore = score.load() if prevScore then score.set(prevScore) end end return true end function chopFruit(fruit) if (sampleVar == true) then score.add(1) playRandomChoppedSound() createFruitPiece(fruit, "top") createFruitPiece(fruit, "bottom") createSplash(fruit) createGush(fruit) fruit:removeSelf() else end end 

local function main() display.setStatusBar( display.HiddenStatusBar ) setUpBackground() --livesLabel = display.newText("lifeCounter: ", \_W\*0.3,\_H\*0.3, native.systemFont, 16) --livesText = display.newText("Lives: 0", \_W \*0.3, \_H\*0.3, "Pacifico", 22) --livesText:setTextColor(1, 1, 1) --livesText.text = ("Lives: " )..lives pauseAndResume () setUpCatchPlatform() initGroups() initFruitAndSplash() Runtime:addEventListener("touch", drawSlashLine) startGame() local scoreText = score.init({ fontSize = 32, font = "Pacifico", x = \_W \* 0.5, y = \_H \* 0.15, maxDigits = 5, leadingZeros = true, filename = "scorefile.txt", }) end 

function displayGameOver() -- Will return a group so that we can set the alpha of the entier menu group = display.newGroup() -- Dim the background with a transperent square local scoreText1 = score.init({ fontSize = 32, font = "Pacifico", x = \_W \* 0.63, y = \_H \* 0.49, maxDigits = 4, leadingZeros = true, filename = "scorefile.txt", }) saveButton = widget.newButton({ width = 200, height = 64, x = display.contentCenterX, y = display.contentHeight - 64, label = "Load Score", labelColor = { default = { 1, 1, 1 }, over = { 0, 0, 0 } }, fontSize = 32, font = "Pacifico", onEvent = loadScore }) local gameOverbg = display.newImageRect( "bg.png", \_W,\_H) gameOverbg.x = \_W \* 0.5 gameOverbg.y = \_H \* 0.5 group:insert(gameOverbg ) local gameOverboard = display.newImageRect( "gameoverboard.png", 357,187) gameOverboard.x = \_W \* 0.5 gameOverboard.y = \_H \* 0.5 group:insert(gameOverboard) local gameOver = display.newImageRect( "gameover.png", 168,52) gameOver.x = \_W \* 0.4 gameOver.y = \_H \* 0.37 group:insert(gameOver) highscore = display.newImageRect( "highscore.png", 168,52) highscore.x = \_W \* 0.45 highscore.y = \_H \* 0.52 group:insert(gameOver) local replayButton = widget.newButton { width = 130, height = 42, defaultFile = "replayButton.png", overFile = "replayButtonover.png", label = "", onRelease = function(event) group:removeSelf(); main() ;removereset(); end } group:insert(replayButton) replayButton.x = \_W \* 0.62 replayButton.y = \_H \* 0.64 score.save() return group end  

and I’ve got the score.lua from the tutorial 

also I’m having to press the load.score button to make the score show on the replay screen , hopefully its an easy fix :) 

i have fixed the score updating issue , i placed this code in the game over function ,

local prevScore = score.load(

   if score.get() > prevScore then

        score.save()

   end

 

but I’m still having the problem where the highscore don’t load in the replay menu until i click the load score button

 

do you know anyway i could make it load automatically with the game over function

False alarm it’s still not working , when I run the code it says error comparing nil to number so it’s saying my Prev score is nil because he hasn’t saved a score yet , how do I correct this so it saves his score on his first go so it creates a save file and then every go after wards it compares the scores and saves if the score is higher than the high score ?

Bump

Can you copy/paste the error message (and any other relevant messages in your console log)?

It should give you a line number where the error happens (and module)?  What line of code is that?

Rob

the error is 

File: game.lua

Line: 366

Attempt to compare nil with number

and this is the code where it creates an error, the error starts on the line " if score.get() > prevScore then score.save() end

local prevScore = score.load() if score.get() \> prevScore then score.save() end return group end

and in the console it comes up Error: could not read nil .

i think its because the score file hasn’t been created yet, so it can’t read from it so its showing up nil,  but i aint sure how i can save the score before the person playing dies 

You have to test for that…

 local prevScore = score.load() if prevScore then if score.get() \> prevScore then score.save() end else score.save() end return group 

or something like that.

Rob

i have fixed the score updating issue , i placed this code in the game over function ,

local prevScore = score.load(

   if score.get() > prevScore then

        score.save()

   end

 

but I’m still having the problem where the highscore don’t load in the replay menu until i click the load score button

 

do you know anyway i could make it load automatically with the game over function

False alarm it’s still not working , when I run the code it says error comparing nil to number so it’s saying my Prev score is nil because he hasn’t saved a score yet , how do I correct this so it saves his score on his first go so it creates a save file and then every go after wards it compares the scores and saves if the score is higher than the high score ?

Bump

Can you copy/paste the error message (and any other relevant messages in your console log)?

It should give you a line number where the error happens (and module)?  What line of code is that?

Rob

the error is 

File: game.lua

Line: 366

Attempt to compare nil with number

and this is the code where it creates an error, the error starts on the line " if score.get() > prevScore then score.save() end

local prevScore = score.load() if score.get() \> prevScore then score.save() end return group end

and in the console it comes up Error: could not read nil .

i think its because the score file hasn’t been created yet, so it can’t read from it so its showing up nil,  but i aint sure how i can save the score before the person playing dies 

You have to test for that…

 local prevScore = score.load() if prevScore then if score.get() \> prevScore then score.save() end else score.save() end return group 

or something like that.

Rob