- Home /
Problem with adding score to leaderboard
So I have a leaderboard in my game. When a player has finished and their game is over, it should add their score to the leaderboard. If it's a new highscore, their highscore will be changed and updated to the leaderboard and if it isn't a new highscore, the players highscore will remain the same until it is beaten. For some reason when adding a player's name and their highscore, it will only add the name and give the leaderboard entry a score of 0. I have debugged and everything comes up correct so I seem to be referencing everything correctly so I don't know what I'm doing wrong. Here is the adding to leaderboard code below:
player.coins = player.coins + player.thisGameCoins;
if(scoreManager.newHighscore == true)
{
player.highscore = finalScore;
}
highscoresManager.AddNewHighscore(menu.userName, player.highscore);
gameManager.SavePlayerData();
The weird thing is, even if I replace the player.highscore with a hardcoded integer like this:
highscoresManager.AddNewHighscore(menu.userName, 10);
it will work perfectly and add the value of that integer to the leaderboard so I do not know what I am doing wrong, any help would be appreciated :)
Can we take a look at the implementation of AddNewHighscore? As you've tested with a hardcoded value, likely your issue is with writing or reading this data. There doesn't seem to be any logical issue here so without more information it's impossible to tell.
public void AddNewHighscore(string username, float score)
{
StartCoroutine(UploadNewHighscore(username, score));
}
IEnumerator UploadNewHighscore(string username, float score)
{
WWW www = new WWW(webURL + privateCode + "/add/" +
WWW.EscapeURL(username) + "/" + score);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
print("Upload successful!");
}
else
{
print("Error uploading: " + www.error);
}
}
I'm currently using the dreamlo leaderboard which is storing data online
Answer by Megaboy238 · Feb 06, 2021 at 09:43 PM
Hard to see from what there is to see, the error is probably with the creation of theplayer.highscore or finalSore variable if we can see that please. Have you tried logging it to the console Debug.Log(finalScore); and player.highscore to make sure it has a value and its type int/float etc
The final score is the value used whenever the player has beaten their previous highscore. If the player hasn't beaten it, then the highscore will remain the same. Yes I have debugged it and it is giving me the correct value back.
public float finalScore;
public void Update()
{
finalScore = Vector3.Distance(scorePos.transform.position, acid.deathPosition.position);
finalScore = $$anonymous$$athf.Abs((scorePos.transform.position - acid.deathPosition.position).y / 5);
score.text = finalScore.ToString("0") + "m";
}
This is just a variable that displays the final score at the game over menu
only thing i see different is that the documentation uses an int for sore in script and database, you seem to be using float is all the dreamlo scripts using float and the database.
Yeah you were right, had to convert finalScore to int whoch worked perfectly. Thank you!