Highscore with localstorage()

Hello guys,

I hope that you have wonderfull monday, but i need some help to solve my probem in coding. I have a simple basketball game, where player just throwing ball to hoop and get some points. I want to save this points to high score with local storage. But something doesnt work.

Here is the first code to checking if storage are avallible

function storageAvalible(type)  {
    try {
        var storage = window[type],
            x= '__storage_test__';
        storage.setItem(x.x);
        storage.removeItem(x);
        return true;
    }
    catch (e) {
        return false;
    }
    
}

than I have code with points,which works well, but in initialize i want to set high score

PointCounter.prototype.initialize = function() {

    PointCounter.points = 0;
    this.myhighscore = 0;
    this.highscoretext = this.app.root.findByName('Highscore');
    
    if(storageAvalible ('localStorage')) {
        
        this.myhighscore =localStorage.getItem ('High Score');
        
        if(this.myhighscore === null) {
             
            this.myhighscore = 0;
            
            this.highscoretext.element.text = 'Highscore : ' + this.myhighscore;
            
        }
        
    }
    
    
    if(PointCounter.points > this.myhighscore) {
        
        this.myhighscore = PointCounter.points;
        
        if(storageAvalible ('localStorage')) {
            
            localStorage.setItem ('High Score', PointCounter.points.toString());
             this.highscoretext.element.text = 'Highscore : ' + this.myhighscore;
            
        }
        
    }

In game that just write: Highscore : (and nothing) all the time, so i dont know why its not working.
If you have some solution on this, please try to figure out with me how i can do that.
Thanks for your trying.

Hi @RogerDat,

Try using the browser console to see if there is actually something saved in local storage.

Open the console while your game is running and type:

localStorage;

Then expand the object that is returned and check if there is a value for key ‘High Score’.

I am going to try, i ll give you answer.

1 Like

it just wrote : storage {lenght : 0}

it seems that the code not working right

Yeah, I am not sure about your StorageAvailable() method. Try simplifying your code a bit and see if it saves anything.

You can use the browser debug tools to add a breakpoint and see if the setItem() method actually executes (check a tutorial online on how to set breakpoints in the browser, there are plenty).

Here is a simplified version for testing:

// --- do check here if this if condition is correct
if(PointCounter.points > this.myhighscore) {
    
    this.myhighscore = PointCounter.points;
        
    localStorage.setItem ('High Score', this.myhighscore.toString());
    this.highscoretext.element.text = 'Highscore : ' + this.myhighscore;

}
2 Likes

Ok, i am already simplifying my code, so i deleted this checking avalible the storage, but now again in console that wrote "storage {lenght:0}, and in game that wrote in my text element, Highscore: null

It seems that on some reason that doesnt save data in Local storage right?

here is the code now :

PointCounter.points = 0;
this.myhighscore = 0;
this.highscoretext = this.app.root.findByName(‘Highscore’);

    this.myhighscore =localStorage.getItem ('High Score');
    
    
        
        this.highscoretext.element.text = 'Highscore : ' + this.myhighscore;
        
    
    



if(PointCounter.points > this.myhighscore) {
    
    this.myhighscore = PointCounter.points;
    
   
        
        localStorage.setItem ('High Score', this.myhighscore.toString());
         this.highscoretext.element.text = 'Highscore : ' + this.myhighscore;
        
    
    
}

I’d still advice to check with the debugger if your code actually runs. Add a breakpoint and see if your if statement is validated correctly.

To see if localStorage actually works, you can run this command in the console:

localStorage.setItem ('High Score', '11');

And then check the entries in the local storage object:

localStorage;

Ok, now i wrote it to console, and than console wrote me {High score: ‘11’, length: 1).

But i also try to save this ‘11’ to my code and that doesnt help. Its also null like before.

So what should i do now?
I know that localStorage save data as String right? shouldnt I do some stringify or ?

I can give you acess to project if you want check. Cause i dont know what is wrong.

I already give this code to update function and now its working. It should be in update?

No, that means most likely you’re code in the initialize method never executes.

Check with the debugger or using console logs if it does, that’s a valuable skill to learn.

1 Like

Ok now i was consoling on start game, which score i have now, it gives me back that i have 12 points, which is right cause my highscore allready is 12.

But i also consoling in this function:

if(PointCounter.points > this.myhighscore) {

    this.myhighscore = PointCounter.points;
    console.log('My score is' + this.myhighscore);
        
        localStorage.setItem ('High Score', this.myhighscore.toString());
        this.highscoretext.element.text = 'Highscore : ' + this.myhighscore;
    }

and it gives me nothing, so the problem is in this function right?

And i wrote to console " localStorage.clear();", and again it is Highscore : 0, and on console storage {length:0}

why its not working on initialize but its working on uptade… i am trying to figure it.

No idea, try sharing a sample project so we can take a look.

Ok, i will create sample project.

Its wierd, cause, i need put in update just function if(Pointcounter.point > this.newHighscore) ==> and its working, other code i have in initialize.

But also i m really thankfull to you for helping me.

1 Like