Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to find highest value in a datastore?

Asked by 4 years ago
Edited 4 years ago

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/

`DataStoreService

DatsStore: Money

Album | Data

Alex | 10

Ella | 15

Donald | 20

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?

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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
0
it works thanks. and since i want players to keep data even if name is changed, i kept their userid, and i just used :GetNameFrmoUserIdAsync(). AlexanderYar 788 — 4y
Ad

Answer this question