I am trying to make a global leaderboard, following this tutorial: Creating Global Leaderboards With GUI!. However, it requires having an ordered datastore of the item (such as ‘points’) that you want to rank. The problem is, I store all of my data in a single datastore, as follows:
local function saveData(player) -- The functions that saves data
local tableToSave = {
player.leaderstats.GoldCoins.Value; -- First value from the table
player.BoughtFiveHourBook.Value; -- Second value from the table
player.FiveHourBookBoostLeft.Value;
player.leaderstats.Highscore.Value,
player.Experience.Value;
}
local success, err = pcall(function()
dataStore:UpdateAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
end)
if success then -- If the data has been saved
----print("Data has been saved!")
else -- Else if the save failed
----print("Data hasn't been saved!")
warn(err)
end
end
How could I convert the 4th item in the table (highscore) into an ordered datastore?
Hello! Well I do not believe there is a direct way to transfer it to an OrderedDataStore, and there is no way to save a table to an OrderedDataStore, you can still utilize your table to essentially get the same functionality. If you want to grab the data assigned to a specific entry you are aware of in a table you can use the tables name followed by the position of the data in brackets.
local Highscore = tableToSave[4] -- Retrieving the 4th index from the table
print(Highscore)
Likewise you can also assign names to these entries as well and refrence them the same way. This will make your table a dictionary.
local tableToSave = {
Gold = player.leaderstats.GoldCoins.Value; -- First value from the table
Bought = player.BoughtFiveHourBook.Value; -- Second value from the table
Boost = player.FiveHourBookBoostLeft.Value;
Highscore = player.leaderstats.Highscore.Value,
Experience = player.Experience.Value;
}
local Highscore = tableToSave["Highscore"] -- Retrieving the Highscore index from the table
print(Highscore)
You can also keep track of players stats on the server by creating a new table that retrieves each players saved leaderstat on join. Then sort the new dictionary to get a new organized dictionary.
local Player = game:GetService("Players")
local TempLeaderboard= { } -- Dictionary of players and their stat you want sorted
local function sortLeaderboard(dictionary) -- This function will pass a dictionary through it and return a new sorted dictionary.
local array = {}
for username, value in pairs(dictionary) do
array[#array+1] = {username = username, value = value}
end
table.sort(array, function(a, b)
return a.value > b.value
end)
return array
end
-- This function can also be called anytime in the script when you want to refresh and resort the leaderboard (if using in more than one script, put it in a module script).
-- The function will give you a dictionary sorted in the following fashion.
-- [1] = ▼ {
-- ["username"] = "OurAuthority",
-- ["value"] = 8
-- So you can easily grab the information you need to be displayed on a leaderboard.
local PlayerLeaderboard = {}
Player.PlayerAdded:Connect(function(Player)
TempLeaderboard[Player.Name] = player.leaderstats.Highscore.Value -- Will add player to the server dictionary on join
PlayerLeaderboard = sortLeaderboard(TempLeaderboard) -- The new sorted dictionary
print(PlayerLeaderboard)
end)
-- Here you'll want to do your manual refresh whether that be whenever a players stat changes, or a set time. Just make sure to set PlayerLeaderboard = sortLeaderboard(TempLeaderboard)
Player.PlayerRemoving:Connect(function(Player)
TempLeaderboard[Player.Name] = nil -- Removing the character from the leaderboard when they leave
PlayerLeaderboard = sortLeaderboard(TempLeaderboard) -- Resort the leaderboard
end)
Used the sorting code from this post.
Finally, this is working in my testing and I am quite a new developer. However I do believe you should be able to work this into what you are trying to achieve.