✓ Accepted answer
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)
local richestPeople = pages:GetCurrentPage()
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
1 comment
https://developer.roblox.com/en-us/api-reference/class/OrderedDataStore