How to save the high score of a game to local storage

Hi everyone, I made a retro snake game and I’m trying to save the high score of the game after it finishes. I tried saving it to local storage in my start game function so after the game refreshes it will save the high score but it only logs the entire global object that I have thus not saving the high score after it refreshes. Here is the link to my Codepen, the code is on lines 96-103 on my javaScript file.

It appears that you are only interacting with localStorage after the game is finished. Whenever you start a new game you’ll need to check local storage for a saved game and load that data.

1 Like

Yes, he save only when the isGameOver() function is called. He needs to do something like if(localStorage.getItem(‘gameLogic’) !== undefined) { load the variable values otherwise set as 0, empty object or whatever } at the beginning of the script, with the initialization of the script.

1 Like

Thanks for your responses folks! I was able to solve the problem.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.