How to save game state with LocalStorage API?

I need help making save files which can be selected and will save the last level you beat.
I have know I will have to use cookies but I don’t know how to do so I was hoping that someone could help.


Hello to save something
localStorage.setItem(‘name’,; where is the variable you want save
to load something
localStorage.getItem(‘name’) to recover the variable

Hi @ayrin would I be able to call the variable in a different script?

also would it work like this?


if (LVL = 1) {
sceneName = ‘lvl 2’

If you’re looking for an example of using the LocalStorage API, check out the Flappy Bird project:

It’s used in the game.js script:

Checking for availability of the API:

// The correct way to verify the availability of the local storage API taken from:
function storageAvailable(type) {
    try {
        var storage = window[type],
            x = '__storage_test__';
        storage.setItem(x, x);
        return true;
    catch (e) {
        return false;

Loading the high score from LocalStorage on game startup:

    this.bestScore = 0;
    if (storageAvailable('localStorage')) {
        this.bestScore = localStorage.getItem('Flappy Bird Best Score');
        if (this.bestScore === null) {
            this.bestScore = 0;

Updating the stored high score after each game:

        // Check if we have a new high score and write it to local storage
        if (this.score > this.bestScore) {
            this.bestScore = this.score;
            if (storageAvailable('localStorage')) {
                localStorage.setItem('Flappy Bird Best Score', this.score.toString());

Hi @will thanks for the help but I’ve done a some tests with it and I think I’m getting the hang of it.

but while I have your attention can you help me here Multiplayer in csgo

hi again @will I was wondering what does this error mean?

It says failed to execute 'setItem' on 'Storage'. So presumably, you’re doing something like:

Storage.setItem(key, value);

Instead of:

localStorage.setItem(key, value);

oh I forgot the value thanks for the help

hey @will Now there is 2 of the player and causes a lot of lag can you take a quick look over my code?,41517491

Fix the part in ontriggerenter where it says if “LVL1”.

    var lvlone = localStorage.getItem('LVL1');
    if (lvlone) {
        NUN = false;
          setTimeout(function (){ 
          }, 1000);

ok thanks

without quotes.

thanks again

1 Like

but it did not do anything it’s still the same. @Fus_ion

this isn’t solved though?

just do this instead:

LevelLoader.prototype.initialize = function(){
     this.scene = "";

LevelLoader.prototype.onTriggerEnter = function(dt) {
   var lvlone = localStorage.getItem('LVL1');
    if (lvlone === true && this.scene !== "lvl1") {
             this.scene  = "lvl1";

@Fus_ion it does nothing

you have to replace your old code

@Fus_ion I did and it still did nothing