How do I save in firebase only when the score is greater than or equal to the best score?

0 favourites
From the Asset Store
Best car suspension with spring effect and very cool terrain generation.
  • I am trying to make a leaderboard using firebase as the database. It already saves a username and a score in the database, using these ajax parameters:

    I don't want it to save when it has a lower score; only save when it is a score greater than or equal to the best score

    How can I do this? Thanks for your help.

  • thanks for your answer but i am not using mysql or php

  • You are, just not wittingly.

    Have you considered just getting the high score value on the start of the layout, loading it as a variable, and if your user surpasses that value, update via the ajax call? (you could even check/retrieve it prior to a post if you wanted to get really picky)

    I'm not super familiar with firebase's setup, but a stored value is a stored value.

  • Use the Security Rules to do that

    Something like this-

    		".write": "newData.child('score').val() > data.child('score').val()"

    Allow writes only when New Data Value is greater than Current Data Value.

    The dollar symbol "$" means a wildcard location.

    Read more about rules:

  • Hi Sparsha!! Thanks for your answer

    I tried the code and if it works, but now the problem is that it only allows writing if there is previously a record in the database, if you try to save a new record, it does not allow saving nothing. I'm reading the information about the rules and trying to test variants but still can't get it to save new users.

  • Oops, forgot sorry, try this-

    		".write": "!data.exists() || (newData.child('score').val() > data.child('score').val())"

    Now this allows either if no data exists or if the new data > old data

  • Sparsha you are the master!!

    I tried the code and it works!!

    Now save new records and update only if the score is higher. Thank you very much for your help.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I found an error, and it is that it does not allow to save beyond the number 9, if the score reaches 10 or more, these scores are not saved.

  • Make sure your score is an integer and not a string

  • Yes, the score is stored in a variable type number and the username in a variable string. These are the images:

    Layout 1

    Layout 2

    Firebase rules

  • You have got some extra double-quotes " " which convert your integer to string.

    Make it like this: "{ ""score"": " & scoreVariable & "}"

    Have a look at your Database Console. This is what a Number VS String looks like. Notice the double-quotes " "

  • I tried it and it works correctly!!

    I am very grateful to you for the help.

  • Good morning! Could you post an example file to make a leaderboard in firebase? or tell me please what video tutorial I can learn to do as you do? I want to add a leaderboard in my game and I would like to do it with firebase. Thank you very much in advance!

  • Hi, I am learning about how to save in firebase and what I have achieved has been watching tutorials found here in the forum and asking, as in this case. What I have is the way to save the data and that it is only saved if the score is higher, I am investigating how to display that data. But if it helps you, I have this file with what I have learned so far.

    Also, Sparsha has some excellent plugins:

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