I have the score saving, but I need the upgrades from the “shop” to save when you exit the tab.
https://replit.com/@BluebayStudios/Ples-nerd#script.js
let nerd = localStorage.getItem("nerd") ? parseFloat(localStorage.getItem("nerd")) : 0;
const scoreElement = document.getElementById("score");
const nerdImage = document.getElementById("nerd-image");
updateScore();
nerdImage.addEventListener("click", increaseScore);
function increaseScore() {
nerd++;
updateScore();
}
function buyItem(price) {
if (nerd >= price) {
nerd -= price;
updateScore();
localStorage.setItem("nerd", nerd);
if (price === 50) {
setInterval(() => {
nerd += 0.5;
updateScore();
localStorage.setItem("nerd", nerd);
}, 1000);
}
} else {
alert("You don't have enough nerd to buy this item!");
}
}
function updateScore() {
scoreElement.textContent = `nerd: ${nerd}`;
}
Sky
3
What do you mean exactly?
If you go to ples-nerd.Bluebaystudios.repl.co, when you refresh the page, your points will save, but the upgrades you buy will not.
1 Like
Sky
5
I just made some changes to the code, this should fix your issue:
let nerd = localStorage.getItem("nerd") ? parseFloat(localStorage.getItem("nerd")) : 0;
let hasUpgrade = localStorage.getItem("hasUpgrade") === "true" || false;
const scoreElement = document.getElementById("score");
const nerdImage = document.getElementById("nerd-image");
updateScore();
nerdImage.addEventListener("click", increaseScore);
if (hasUpgrade) {
startUpgradeInterval();
}
function startUpgradeInterval() {
setInterval(() => {
nerd += 0.5;
updateScore();
localStorage.setItem("nerd", nerd);
}, 1000);
}
function increaseScore() {
nerd++;
updateScore();
localStorage.setItem("nerd", nerd);
}
function buyItem(price) {
if (nerd >= price) {
nerd -= price;
updateScore();
localStorage.setItem("nerd", nerd);
if (price === 50) {
startUpgradeInterval();
hasUpgrade = true;
localStorage.setItem("hasUpgrade", "true");
}
} else {
alert("You don't have enough nerd to buy this item!");
}
}
function updateScore() {
scoreElement.textContent = `nerd: ${nerd}`;
}
2 Likes
Thank you! This works great.
One question, when you buy multiple upgrades, only one saves. Is there a solution to this?
1 Like
Sky
8
Yes there is, so you want upgrades to stack up?
Yes, that would be great.
1 Like
Sky
10
Here, this should stack upgrades:
let nerd = localStorage.getItem("nerd") ? parseFloat(localStorage.getItem("nerd")) : 0;
let upgradeCount = localStorage.getItem("upgradeCount") ? parseInt(localStorage.getItem("upgradeCount")) : 0;
const scoreElement = document.getElementById("score");
const nerdImage = document.getElementById("nerd-image");
let upgradeInterval;
updateScore();
nerdImage.addEventListener("click", increaseScore);
if (upgradeCount > 0) {
startUpgradeInterval(upgradeCount);
}
function startUpgradeInterval(upgrades) {
upgradeInterval = setInterval(() => {
nerd += upgrades * 0.5;
updateScore();
localStorage.setItem("nerd", nerd);
}, 1000);
}
function increaseScore() {
nerd++;
updateScore();
localStorage.setItem("nerd", nerd);
}
function buyItem(price) {
if (nerd >= price) {
nerd -= price;
updateScore();
localStorage.setItem("nerd", nerd);
if (price === 50) {
upgradeCount++;
localStorage.setItem("upgradeCount", upgradeCount);
startUpgradeInterval(upgradeCount);
}
} else {
alert("You don't have enough nerd to buy this item!");
}
}
function updateScore() {
scoreElement.textContent = `nerd: ${nerd}`;
}
So, i have to admit. I heavily relied on ghostwriter for adding upgrades, could you help with adding new upgrades?
1 Like
How I had the first upgrade, I want the second one to do the same thing, but more points, and learn how to add more upgrades in the future.
1 Like
I would like the new one to be 10 per second. And have compatibility to save and stack.
system
Closed
15
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.