# Cannot load localStorage

**URL:** <https://phaser.discourse.group/t/cannot-load-localstorage/2931>\
**Category:** Phaser 3\
**Created:** [June 28, 2019, 10:22am UTC](https://phaser.discourse.group/t/cannot-load-localstorage/2931 "2019-06-28T10:22:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benstrocity](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@Benstrocity](https://phaser.discourse.group/u/Benstrocity)\
**Post date:** [June 28, 2019, 10:22am UTC](https://phaser.discourse.group/t/cannot-load-localstorage/2931/1 "2019-06-28T10:22:31Z")

</div>

So I thought that localStorage stored all of your data unless you clear your browser cache. I’m doing a `localStorage.setItem("playerData", playerData)` where playerData points to my playerData object that stores my data. If I `setItem` in the Tutorial level, I’m able to `getItem` in the next level, no problem. But then if I go to my main menu and I click my Load Save button which on pointerdown has an event that `this.scene.start(playerData.level)` where `playerData.level` is a string that changes when you reach a new level to the appropriate super() keyword value is updated in the playerData, my scene will not load. I did a `console.log(playerData.level)` on my menu scene and it sees my `playerData.level` string as empty. So it has no scene to load.

Anything I’m missing?

---

<div class="post-metadata">

**Author:** ![Benstrocity](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@Benstrocity](https://phaser.discourse.group/u/Benstrocity)\
**Post date:** [June 28, 2019, 12:59pm UTC](https://phaser.discourse.group/t/cannot-load-localstorage/2931/2 "2019-06-28T12:59:41Z")

</div>

Figured it out. I just had to change some things around for `this.scene.start()`

When I passed `localStorage.getItem("playerLevel")` to `this.scene.start()` everything worked great.

I created another `localStorage.setItem("playerLevel", playerData.level)` so I think I need to store individual properties from my playerData file with separate keys.

---

<div class="post-metadata">

**Author:** ![ldd](https://avatars.discourse-cdn.com/v4/letter/l/9de0a6/32.png) [@ldd](https://phaser.discourse.group/u/ldd)\
**Post date:** [June 28, 2019, 4:01pm UTC](https://phaser.discourse.group/t/cannot-load-localstorage/2931/3 "2019-06-28T16:01:46Z")

</div>

1. are you sure you are cleaning up your click events properly? It is entirely possible that you have multiple listeners. Add a `console.log` on your pointer down event and check it out.

2. Always use `JSON.stringify` and `JSON.parse` when using localStorage.  
E.g:  
`localStorage.setItem('p', JSON.stringify(p)`  
`p = JSON.parse(localStorage.getItem('p'))`

---

<div class="post-metadata">

**Author:** ![Benstrocity](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@Benstrocity](https://phaser.discourse.group/u/Benstrocity)\
**Post date:** [June 28, 2019, 5:04pm UTC](https://phaser.discourse.group/t/cannot-load-localstorage/2931/4 "2019-06-28T17:04:16Z")

</div>

So I shouldn’t use a javascript object for data storage, it has to be JSON? That’s fine and I can modify what I have but the javascript object seems to work great.

---

<div class="post-metadata">

**Author:** ![prob](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/prob/32/628_2.png) [@prob](https://phaser.discourse.group/u/prob)\
**Post date:** [June 28, 2019, 5:24pm UTC](https://phaser.discourse.group/t/cannot-load-localstorage/2931/5 "2019-06-28T17:24:48Z")

</div>

LocalStorage only holds string key/value pairs.

---

<div class="post-metadata">

**Author:** ![ldd](https://avatars.discourse-cdn.com/v4/letter/l/9de0a6/32.png) [@ldd](https://phaser.discourse.group/u/ldd)\
**Post date:** [June 28, 2019, 5:40pm UTC](https://phaser.discourse.group/t/cannot-load-localstorage/2931/6 "2019-06-28T17:40:56Z")

</div>

Using a Javascript object for data storage is great, and you do not need to change that.

All that you need to know is that to save it to localStorage and load it from localStorage, you need to stringify it and parse it when needed.

---

<div class="post-metadata">

**Author:** ![Benstrocity](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@Benstrocity](https://phaser.discourse.group/u/Benstrocity)\
**Post date:** [June 28, 2019, 6:18pm UTC](https://phaser.discourse.group/t/cannot-load-localstorage/2931/7 "2019-06-28T18:18:06Z")

</div>

Hmm, okay. Its so interesting. It’s working exactly how I would expect without having to stringify and parse. But I’ll look up stringifying and parsing javascript objects to make it correct.
