Beginner learning from forums websites and youtube, bear with me pls.
(plus im dumb)
I’m making a stupid clicker game just to see if i can do it (clearly not).
I’ve got some buttons with onclick functions and multipliers setup whatever everything works fine.
Until Saving/Loading.
I followed a tutorial i found for the save function, to save/load your progress via localStorage/cookies, and it’s saving the total “money” (score) upon refresh, but my “scorepersecond” and every upgrade via button doesn’t save, even though i’ve included their cost and name values to tell which variables i want to save. I’m using VS Code with Live Server extension to test (firefox).
function loadGame() {
var savedGame = JSON.parse(localStorage.getItem("gameSave", "score", "scorepersecond", "cursors", "cursorcost", "lighters", "lightercost", "grinders", "grindercost", "devils", "devilcost", "trays", "traycost", "dealers", "dealercost"));
if (typeof savedGame.score !== "undefined") score = savedGame.score;
}
function saveGame() {
var gameSave = {
score: score,
scorepersecond: scorepersecond,
clickingPower: clickingPower,
cursors: cursors,
cursorcost: cursorcost,
lighters: lighters,
lightercost: lightercost,
grinders: grinders,
grindercost: grindercost,
devils: devils,
devilcost: devilcost,
trays: trays,
traycost: traycost,
dealers: dealers,
dealercost: dealercost,
};
localStorage.setItem("gameSave", JSON.stringify(gameSave));
document.getElementById("score").innerHTML = score;
document.getElementById("scorepersecond").innerHTML = scorepersecond;
document.getElementById("cursorcost").innerHTML = cursorcost;
document.getElementById("cursors").innerHTML = cursors;
document.getElementById("lightercost").innerHTML = lightercost;
document.getElementById("lighters").innerHTML = lighters;
document.getElementById("grindercost").innerHTML = grindercost;
document.getElementById("grinders").innerHTML = grinders;
document.getElementById("devilcost").innerHTML = devilcost;
document.getElementById("devils").innerHTML = devils;
document.getElementById("traycost").innerHTML = traycost;
document.getElementById("trays").innerHTML = trays;
document.getElementById("dealercost").innerHTML = dealercost;
document.getElementById("dealers").innerHTML = dealers;
}
window.onload = function() {
loadGame();
updatescorepersecond();
document.getElementById("score").innerHTML = score;
document.getElementById("cursorcost").innerHTML = cursorcost;
document.getElementById("cursors").innerHTML = cursors;
document.getElementById("lightercost").innerHTML = lightercost;
document.getElementById("lighters").innerHTML = lighters;
document.getElementById("grindercost").innerHTML = grindercost;
document.getElementById("grinders").innerHTML = grinders;
document.getElementById("devilcost").innerHTML = devilcost;
document.getElementById("devils").innerHTML = devils;
document.getElementById("traycost").innerHTML = traycost;
document.getElementById("trays").innerHTML = trays;
document.getElementById("dealercost").innerHTML = dealercost;
document.getElementById("dealers").innerHTML = dealers;
};
Thing’s I’ve Tried.
var savedGame = JSON.parse(localStorage.getItem("gameSave"));
if (typeof savedGame.score !== "undefined") score = savedGame.score;
}```
```function loadGame() {
var savedGame = JSON.parse(localStorage.getItem("gameSave"));
if (typeof savedGame.score !== "undefined") score = savedGame.score;
if (typeof savedGame.scorepersecond!== "undefined") scorepersecond = savedGame.scorepersecond;
}
along with other variables i want to save, in the form of “(typeof ” which have also failed.
Not sure if this bit literally means anything, but if i inspect element on the page and access local storage it updates accordingly every 30 seconds with proper values, but upon page refresh it poof’s (except for total “money” (score) on top of the page).
Not sure what people need to see or if i shared enough, but if anyone could help without being a d-bag that’d be cool 🙂 even if not to solve it but a general idea of where i f’d up besides the whole thing. thanks in advance and lmk if you need to see more of my poopy clicker game.