How to use the "locale storage" to create save and load function?

First time on this forum, so I am VERY new to JS just started like one days ago. I was following this tutorial:https://kastark.co.uk/articles/incrementals-part-2.html to try and create an incremental game, I did all the previous parts correctly and had a basic understanding of them, but when it came to the save and load function it just wouldn't work, I am stuck on this for hours now. I am probably just too oblivious to notice what is wrong.

function save() {
    var save = {
        cookies: cookies,
        cursors: cursors,
        prestige: prestige,
    }
    localStorage.setItem("save",JSON.stringify(save));
}

function load() {
    var savegame = JSON.parse(localStorage.getItem("save"));
    if (typeof savegame.cookies !== "undefined") cookies = savegame.cookies;
    if (typeof savegame.cursors !== "undefined") cookies = savegame.cursors;
    if (typeof savegame.prestige !== "undefined") cookies = savegame.prestige;
}

This is the html

<html>
    <head>
        The game
        <br/>
    </head>
    <body onload="load()">
        <button onclick="cookieClick(1)">Gain Cookie!</button>
        <br />
        Cookies: <span id="cookies">0</span>
        <br />
        <button onclick="buyCursor()">Buy Cursor</button>
        <br />
        Every cursor give you 1 cookie per second! How sweet!
        <br />
        Cursors: <span id="cursors">0</span>
        <br />
        Cursor Cost: <span id="cursorCost">10</span>
        <br />
        <button onclick="save()">Save</button>
        <br />
        Remember to Save!
        <script type="text/javascript" src="Function.js"></script>
    </body>
</html>


Read more here: https://stackoverflow.com/questions/68123990/how-to-use-the-locale-storage-to-create-save-and-load-function

Content Attribution

This content was originally published by Orange Inks at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: