Skip to content
← Back to Community
Live Leaderboards
Profile icon
MicroSoftHelp

I am working on this game, and would like to know how to store scores for a leaderboard, maybe a .txt file or something?

Voters
Profile icon
dudeactualdev
Profile icon
ReyesP
Profile icon
MicroSoftHelp
Comments
hotnewtop
Profile icon
Spacecraft

ReplitDB does not support client usage in console programs. The solutions provided below will work for you only, but not for anyone else who runs your program.

Profile icon
RYANTADIPARTHI

Use the db

from replit import db db[something] = something

in case of something, put your variables.

Profile icon
MicroSoftHelp

@RYANTADIPARTHI How do I display this?

Profile icon
RYANTADIPARTHI

@OperationDemon You print it out.

print(something)

since something is now assigned to it.

Profile icon
Coder100

yeah, but you could use some technology, and use repl.it db

from replit import db db['USERNAME'] = SCORE ...
Profile icon
Kai_Justice

ReplDB is easy and would be your best bet.

ex.

from replit import db db["leaderboard"].append(*user score, username, etc*)
Profile icon
MicroSoftHelp

@Kai_Justice I will try that!

Profile icon
MicroSoftHelp

@Kai_Justice How do I display this?

Profile icon
Kai_Justice

@OperationDemon

leaderboard = db["leaderboard"] leaderboard.sort(reverse=True) print('\n'.join(leaderboard))

Though I suggest some experimentation to find out what looks best.

Profile icon
MicroSoftHelp

@Kai_Justice Where should I put this in my code, and how do I incorporate the username and score?

Profile icon
Kai_Justice

@OperationDemon
Run this once then delete it:

db["leaderboard"] = [] # Clears the leaderboard and sets it to a list

Then put this right after from replit import db:

leaderboard = db["leaderboard"] # assigns the leaderboard list to the variable 'leaderboard'

To add a score to the leaderboard:

db["leaderboard"].append(("Name of user", score)) # Appends the a tuple containing the name of the user and their score

To display the leaderboard:

leaderboard.sort(reverse=True, key=lambda x: x[1]) # Make sure the variable 'leaderboard' is assigned before this statment # Easy way for index, user in enumerate(leaderboard): # Since the values in the leaderboard are tuples you can access them like so print(index, user[0], user[1]) # user[0] = username # user[1] = score # Complex way print('\n'.join([f"{index}. {user[0]} {user[1]}" for index, user in enumerate(leaderboard)]))
Profile icon
MicroSoftHelp

@Kai_Justice How do I allow for them to put their username?

Profile icon
Kai_Justice

@OperationDemon
At the start of your code:

username = input("Username : ")

Then replace "Name of user" with username.

Profile icon
MicroSoftHelp

@Kai_Justice Where do I make it so it stores the variables of the username and score?

Profile icon
Kai_Justice

@OperationDemon Like how you store any other variable?

edit: Nvm just put them in a tuple and append to db["leaderboard"] like so db["leaderboard"].append((username, score))

Profile icon
MicroSoftHelp

@Kai_Justice i probably sound like an idiot, but what is a tuple?