WebStorage Score 'NAN' Error - Please Help!

0 favourites
  • 11 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • I have spent a couple of days now trying to integrate the webstorage option into my game to save a players best previous score.

    I've followed several tutorials and also read a lot of stuff on the forum which has helped me greatly but I have hit a wall now and could really do with a little bit of help please.

    Basically I have the actual score working correctly and the 'Best Score' adds up correctly too but when you exit and reload the game the best score just says NAN. I gather that means 'Not A Number' so it might just be something silly but I'm not sure what I need to change!

    Here is the relevant code from Construct:


    And here is the error message in game:


    If anyone can help me get this working it would be greatly appreciated. This is my first try at a Construct game so it's all a learning experience but I'm getting there!

    Many thanks.

  • Webstorage only stores strings.

    First I would suggest to have a read on this: https://www.scirra.com/manual/120/webstorage

    After that, convert your number to string before saving it and on load, turn it back into an integer <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

  • Hi 2retro, got your pm, I am taking a break from helping in forums due to some members who pushed too far.

    I see you got a reply. So just to add to it int(x) changes to whole number and str(x) changes to string. X being whatever you want to change.

    Recommend reading this as it is the most important page in the manual - lol.

    Good luck. And you are welcome, glad the example capx helped you a bit.

    Remember that highscore will only be for that player, meaning each player will have their own highscore. Web Storage saves on players pc, not on the web.

    You can also change your variables to text instead of numbers then they will store as a string too. But remember if you want to treat them as whole numbers you must convert them to numbers (for calculations etc they will need to be numbers, else if for display purposes, then text (string) is fine.

  • Thanks, guys. I'll see if I can put some of this great info into practice and report back!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • OK, I've read through all of the links that were kindly provided to me now an have made some progress:


    I changed the bit with the arrow and that has resolved the NAN error and now retains the best score between sessions which is great.

    But now the best score continually increases along with each point scored. For example, your best score is 10. You play again and it tells you your best score was 10 but then, as you play and score more points, the best score increases by that amount of points:


    Now, I know I need to convert something to 'string' in the area marked with a question mark but I'm getting rather confused!

    If someone could perhaps tell me what I need to change (and to what) in the Scoring area to make it work correctly that would be really great.

    Thanks again.

  • 2retro

    You must only change best score if current score is greater than best score.

    I would create a function called savescore.

    Savescore does this...

    BestScore = CurrentScore

    webstorage set local key "bestscore" to bestscore

    Highscore text = Set "Best:"&BestScore

    (something like that)


    You can compare the two and if score > BestScore then

    Function: savescore

    Well something like this...

    Reason for issue: You are adding 1 to both score and bestscore where you should only be adding 1 to score and when score is better than bestscore, store score as new best score.

    Sorry, I wrote this quickly, its late here, hope its not to confusing - lol

  • Thank you once more for helping me. I *think* I get what you mean. I'll give it a shot and let you know...

  • Hmmm, still a bit confused I'm afraid. I see where I'm going wrong and the reason behind it but can't seem to get it right. How do I create the SaveScore Function for example? Is there a tutorial or capx file to compare against anywhere?

    Maybe you can help me out a bit more tomorrow if you have time.

    Thank you again for everything so far anyway.

  • It doesn't have to be a function it can simply be an event like this (a function would be cleaner though):

    if Score > BestScore

    Set BestScore to Score

    Then the rest should work fine...

  • What bluephaze said.

    You can compare the two and if score > BestScore then

    BestScore = CurrentScore

    webstorage set local key "bestscore" to bestscore

    Highscore text = Set "Best:"&BestScore

    (something like that)

    And remove the bestscore stuff from ? mark piece.

    I use functions as I can reuse the code over and over and over. Maybe a little too advanced for now.

  • Guys, I cannot thank you enough - that's exactly what I was trying to achieve and it works perfectly now! Thanks for all the help.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)