Im making a game, and for a leaderboard, i need to find the highest value in the datastore called "money". For example, this is a qick chart i made/
Obama |12 `
Donald would be the highest, so he would be on leaderboard.
so i want to find the highest number so i can put the name of the datastore with highest number in the leaderboard. How do i find the highest number using script?
Normal data stores can't be sorted by their data, for that you have to use a OrderedDataStore
.
To get an OrderedDataStore
is similar to how you get a normal DataStore
, you would use this code:
local DataStoreService = game:GetService("DataStoreService") local moneyDataStore = DataStoreService:GetOrderedDataStore("Money")
Then, you can use it like an ordinary DataStore
, except it can only hold numbers.
If you wanted to now get a list of the top entries in the OrderedDataStore
, you could use the code
local pages = moneyDataStore:GetSortedAsync(false, 15) -- The first parameter is whether we want it to sort from lowest to highest (we don't, so it's false). The second parameter is how many entries we want per page local richestPeople = pages:GetCurrentPage() -- This will return a table with the first page of the top entries.
You could then just loop through the richestPeople list like a normal table, and it'd be the people with the highest amount of "Money" in the datastore.
Also note, you have to use .key
on the entry to get the name, and .index
to get the money. Therefore, if you wanted to print out the richest people, you would do
for _, entry in pairs(richestPeople) do print(entry.key.." has "..entry.value.." money") end