Open notepad on your computer and create a simple text file.
In that text file type 0
That’s a zero by the way, and save it to your game folder where your Game1.lua is and call it highScore.txt
If you want to start the highScore with a higher number then put that instead of zero when you create it.
In your main.lua put this code in.
local highScore \_path = system.pathForFile( "highScore.txt", system.ResourceDirectory ) \_file = io.open( \_path, "r" ) for line in \_file:lines() do highScore = tonumber (line) end io.close( \_file ) \_file = nil \_path = nil \_path = system.pathForFile( "highScore.txt", system.DocumentsDirectory ) \_file = io.open( \_path, "w" ) \_file:write( highScore ) io.close( \_file ) \_file = nil \_path = nil
It reads the highScore.txt you just put in your game folder and gives the local variable highScore the value you typed in before.
It then writes the highScore.txt to the system.Documents directory where it needs to be when you build the game for the device, don’t worry about this part.
At the top of every module in your game only where you use the score put this:
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
The above loads the highScore.
Keep using the score the way you are now.
In your Game1.lua when the game is over check the score to see if it’s higher than the highScore
if score \> highScore then highScore = score \_path = system.pathForFile( "highScore.txt", system.DocumentsDirectory ) \_file = io.open( \_path, "w" ) \_file:write( highScore ) io.close( \_file ) \_file = nil \_path = nil end
The above writes the new highScore to the highScore.txt document.
“w” means write to and “r” means read from.
That’s the basics, you can easily see how it works and you can add to it all you like, have fun.