Hi All,
Recently I made a datastore system, saving to the datastore through a table, but I need to link it to a leaderboard. How would I get an ordered list of players through a specific value in that table?
Here’s pretty much how I save it (Tell me if you want the full script):
function module.removed(player)
local Stats = player:FindFirstChild("PlayerData")
local LeaderStats = player:FindFirstChild("leaderstats")
local Cameras = player:FindFirstChild("Cameras")
local SDCards = player:FindFirstChild("SDCards")
local SaveData = {
Photos = LeaderStats.Photos.Value,
Coins = LeaderStats.Coins.Value,
Gems = LeaderStats.Gems.Value,
Rank = LeaderStats.Rank.Value,
PhotoMultiplier = Stats.PhotoMultiplier.Value,
MaxPhotos = Stats.MaxPhotos.Value,
TotalPhotos = Stats.TotalPhotos.Value,
PlayTime = Stats.PlayTime.Value,
CurrentCam = Stats.CurrentCam.Value,
CurrentSD = Stats.CurrentSD.Value,
SDCards = {},
Cameras = {}
}
for _,camObject in pairs(Cameras:GetChildren()) do
SaveData.Tools[camObject.Name] = camObject.Value
end
for _,sdObject in pairs(SDCards:GetChildren()) do
SaveData.DNA[sdObject.Name] = sdObject.Value
end
PlayerData:SetAsync(player.UserId, SaveData)
end
I’m basically trying to rank players by their coins value, but I don’t want to have a seperate datastore for coins, as I will be adding gems leaderboards and other things.
Any help would be appreciated!
If you are already using an OrderedDataStore, then you can use GetSortedAsync, to get a DataStorePages object. You can then read this to access the value tables in your OrderedDataStore. Once you have gotten your values, you can simply do whatever you want with them.
Here is a basic example, taken from the Developer Hub page on DataStorePages:
local DataStoreService = game:GetService("DataStoreService")
local PointsODS = DataStoreService:GetOrderedDataStore("Points")
local function printTopTenPlayers()
local isAscending = false
local pageSize = 10
local pages = PointsODS:GetSortedAsync(isAscending, pageSize)
local topTen = pages:GetCurrentPage()
-- The data in 'topTen' is stored with the index being the index on the page
-- For each item, 'data.key' is the key in the OrderedDataStore and 'data.value' is the value
for rank, data in ipairs(topTen) do
local name = data.key
local points = data.value
print(data.key .. " is ranked #" .. rank .. " with " .. data.value .. "points")
end
-- Potentially load the next page...
--pages:AdvanceToNextPageAsync()
end
-- Create some data
PointsODS:SetAsync("Alex", 55)
PointsODS:SetAsync("Charley", 32)
PointsODS:SetAsync("Sydney", 68)
-- Display the top ten players
printTopTenPlayers()
Hope this helps.
1 Like
Thanks, I’ll check it out, looks like that might work 
1 Like
I’m taking a look now, how would I save to a Pages object?
I seem to find it confusing
You don’t save to a pages object. You just use Pages to view all of the values in your OrderedDataStore, in little segments at a time.
You save the same way you would normally, by saving to your players “Key”.
The only thing that changes is how you get, and handle, the data. see bottom of the code I inserted into my original reply for example of saving