Hello! I have been trying to make a website, that acts as a game. When the player moves to the next level, it will add +10 to their progress bar, and they completed the level. I have seen another post about this matter, but it has not been followed up in a couple of days, so I decided to make my own post. I want to make it so the progress bar saves when the other player moves on to the next level. Below is the code I am using.
import {local} from ‘wix-storage’ ;
$w.onReady( function () {
});
export function button1_click_(event){
$w( “#progressBar10” ).value = +10 ;
local.setItem( ‘progress’ , $w( ‘#progressBar10’ ).value);
const progress = local.getItem( ‘progress’ );
$w( ‘#progressBar10’ ).value = progress;
}
(Yes I copied a lot of code from the other user, as someone helped the other user figure out this much)
However, the other users’ code does not work, can someone please help me? I want the progress bar to save and it doesn’t with this code! Another user used a very similar code to me, but their code doesn’t work as well!
Please Help!
The following line sets the progress bar to 10, it does not add 10.
$w("#progressBar10").value = +10;
You can add 10 to the variable x like this:
x += 10;
So, if you want to add 10 to your progress bar, you need to do something like this:
let progress = Number(local.getItem('progress')) + 10;
$w('#progressBar10').value = progress;
Hey, thanks for getting back to me! I used the code
import {local} from ‘wix-storage’ ;
$w.onReady( function () {
});
export function button1_click_(event){
let progress = local.getItem( ‘progress’ ) + 10;
$w( ‘#progressBar10’ ).value = progress
}
but I keep getting this error code
Wix code SDK error: The value parameter that is passed to the value method cannot be set to the value 510. It must be of type number.
Can you please help me?
@tgrtmgmt Sorry, I forgot to mention that values in storage are strings and not numbers. You want to do this:
let progress = Number(local.getItem('progress')) + 10;
Hello! Thanks for getting back to me, however I tried it and my progress for the bar doesnt save. Code is below
import wixLocation from ‘wix-location’ ;
import {local} from ‘wix-storage’ ;
$w.onReady( function () {
});
export function button1_click_(event){
let progress = Number(local.getItem( ‘progress’ )) + 5 ;
$w( ‘#progressBar1’ ).value = progress;
[wixLocation.to(](wixLocation.to(“/Next) [”/Next](wixLocation.to("/Next) Level " );
}
It takes me to the next level, but the progress bar doesn’t save.
Can you please tell me why?
If I understand correctly, it’s because you aren’t saving the value:
local.setItem('progress', $w('#progressBar10').value + '');
I used the code below but it still doesn’t work.
import wixLocation from ‘wix-location’ ;
import {local} from ‘wix-storage’ ;
$w.onReady( function () {
});
export function button1_click_(event){
let progress = Number(local.getItem( ‘progress’ )) + 5 ;
$w( ‘#progressBar1’ ).value = progress;
local.setItem( ‘progress’ , $w( ‘#progressBar1’ ).value + ‘’ );
wixLocation.to( “/NextLevel” );
}
Can someone help?
Just wanted to follow up, just to make sure I get answered! Please, I am still stuck!