How do I save multiple scores to array and local storage?

0 favourites
From the Asset Store
"Easy Local Notifications" enhances games with customizable local notifications.
  • I'm completely new to all of this, but somehow I've managed to almost finish my platformer trivia game. All I need to finish is the leaderboard. After researching, I decided that the best method for our needs was to save scores in an array and use the local storage plug in. But for the life of me, I cannot get it to save multiple scores. Right now, I have an input box to type names and when "submit" is clicked, the text from the box and the player's final score are input into the first text boxes in the leaderboard. That part all works properly. But then when I play again, the new score overwrites the old score rather than adding to the list. And it doesn't seem to be saving to the blank array at all. I feel like that's my problem: saving the data to the array so it opens up next time the game is opened in the same browser. And being able to add to that data without overwriting it. I've read a ton of tutorials and utilized ChatGPT, but so far nothing has worked. The screenshot below shows what I have right now.

    I also have the Localstorage set item "highscores" to leaderboardarray.atJSON further down my event sheet when the scores need to be saved.

    For clarification, the array shown in the first event all works properly. I'm just having issues saving and pulling from my leaderboard array.

    So how should I go about doing this? I feel like I have to be missing something, but I've tried so many different things at this point. Could it just be an order thing? I'm terrible at keeping my event sheet ordered properly...

  • Hi.

    I posted an example of saving and loading using an array and local storage here.

    Maybe it will help.

    construct.net/en/forum/construct-3/how-do-i-8/problems-saving-loading-177672

  • Thank you, but unfortunately that didn't work. It's still doing the same thing where it just overwrites the first score every time. I think I'm just missing something, but I even tried specifying each text object and it still just overwrites the first text box.

  • Ok because I couldn't figure this out, I made a test game to play around with different options and find out what works. Following a forum post about how to make a top 10 leaderboard, I put together the following. It kind of worked initially, but was a bit messy for some reason. So I changed some things and somehow broke it. Now it just replaces the top score every time without moving scores down. If I could get this to work, it at least solves the name part of the leaderboard. I'm not sure how I'm going to keep it linked to player scores, but one step at a time, I suppose.

    Anyway, can anyone tell me what I'm doing wrong in the following event sheet? I just want a top 5 leaderboard with names (and eventually scores, but I haven't gotten that far yet). I want it to save in the array to local storage.

  • I don't have the expertise to explain how to do it.

    But I can do it with my own hands.

    Now I will make and post a simple leaderboard for 5 people.

  • This is an example of a simple leaderboard that allows you to add a new player if his name is not in the leaderboard.

    It displays the top 5 results.

    All data is stored in local storage.

    I hope you will understand the principle and do as you need.

    dropmefiles.net/en/KzvT85UaW

  • There's actually a ton of confusing stuff going on here. From what I can see it looks like the array only contains a list of names not scores? You input a name and then push it to the array, where does the score come in? The save events are kind of important but you didn't share them, so what you are saving is a list of names? We can only guess.

  • lionz

    Yes, like I said in my last post, I've only figured out how to push the names to the array so far. Since I didn't want to mess up my event sheet on my actual game, this is 100% a test game right now, so there's very limited data in it. And using tutorials online, I figured out how to save the names. But then that part seemed to stop working, so I figured I would find out how to do that first and then move on to figuring out how to link the scores to the names after. So yes, this event sheet has only the name part incorporated right now. Does that make sense?

  • I think the main problem is the bottom event is meant to be a sub event. It's ok to run that loop after you update the array but you have it running constantly on its own. Press S and add as a sub event so it runs after you updated the array and only once. Also I suggest setting the text of Name to "" to clear it before running the loop.

    It's confusing because I can't see where you've saved but if 'highscore' in local storage is just the name then you should run the loop again on the event where you get highscore and the array is loaded. The part where it runs on start of layout can be removed as this will not do anything it is using loopindex but not inside a loop plus the array hasn't loaded yet at that point.

  • lionz

    I made all these changes, and it's still just overwriting the first line every time without adding to the list. Interestingly, it pushed down two of the names the first couple run throughs, but stopped after that. Is there something that would cause that? I only made the changes you suggested so those were the only differences to the screenshot I posted above.

  • Do you mean overwriting the first line on the text object? The array is the important part so make sure that's correct. Share a screenshot of how the new events look. If you clear the text each time it should work.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • lionz

    I think I followed all your directions? I was a bit confused by the set text Name to "", but I think I did it how you meant. I tried it both in its present location and in the sub event before the loop. Neither works. So maybe I have it in the wrong place? I also tried without and that just leads to overwriting the first line of the text object. Sorry for being so confused; I've just never done anything like this before.

  • Not sure on overwriting the first line, the events look fine to me except the bit where you set text on getting the local storage item isn't in a loop and it should be, like the other part where you set text using loopindex.

    Is the array always correct? Check the array in debug view when you set the text to see if it matches with the text. If they don't match and it's just a text display issue then it's not the worst bug ever but I couldn't work out why it would skip the first line of the array.

    As the original issue was to do with loading from the array, then make sure that set text is in a loop as mentioned and make sure the array is correct and as you expect on loading storage item.

  • lionz

    After checking it in debug view, the array is not saving correctly. Interestingly, a couple names that I entered using this same method earlier were still in there. So it was apparently saving it properly at some point. So I must've changed something that messed with that. No idea what though. I added the loop like you suggested, but it still doesn't seem to work. I also tried adding a "if missing" from local storage event and pull the array file using AJAX, but still no luck. No idea what I'm doing wrong.

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