DataStoreService - OrderedDataStore for a leaderboard

Hello,

I have some problems with this script, I helped myself with this video (How to Make A Global Leaderboard in Roblox Studio - YouTube) and this page (OrderedDataStore | Roblox Creator Documentation)

When I have managed to solve this problem I will put it in the form of a GlobalLeaderboard.

-- << 	Services >> --
local DataStoreService = game:GetService("DataStoreService")
-- << 	Variables >> --
local data_Donations = DataStoreService:GetOrderedDataStore("DonationsScore")
-- << Functions >> --
local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = data_Donations:GetSortedAsync(false, 10)
		local topTEN = Data:GetCurrentPage()
		
		for rank, data in ipairs(topTEN) do
			local name = data.key
			local score = data.Value
			print(name.." is ranked #"..rank.." with "..score.."score")
		end
	end)
	
	if not success then
		print(errorMessage)
	end
end
-- << Connect >> --
while true do
	updateLeaderboard()
	print("Updated!")
	
	wait(10) -- Temp
end

I have no errors but nothing happens, maybe the data is not recognised? I don’t understand it very well
Thanks!

1 Like

You could always try to replicate it (cloning)

An example that I use: - Refreshes every 20 seconds


-- >> Variable/Services << --

local PrestigeODS = game:GetService("DataStoreService"):GetOrderedDataStore("YOURDATA")
 
-- >> Script << --

local function Handler()
    local Success, Err = pcall(function()
        local Data = PrestigeODS:GetSortedAsync(false, 15)
        local PrestigePage = Data:GetCurrentPage()
        for Rank, Data in ipairs(PrestigePage) do
            local Name = Data.key
            local Prestige = Data.value
            local NewObj = game.ReplicatedStorage:WaitForChild("lbFrame"):Clone()
            NewObj.Player.Text = Name
            NewObj.TotalWheat.Text = Prestige
            NewObj.Rank.Text = "#"..Rank
			NewObj.Position = UDim2.new(0, 0, NewObj.Position.Y.Scale + (0.08 * #game.Workspace.GlobalLeaderboard2.lbGUI.Holder:GetChildren()), 0)
            NewObj.Parent = game.Workspace.GlobalLeaderboard2.lbGUI.Holder
        end
    end)
    if not Success then
        error(Err)
    end
end

-- >> Loop to refresh << --
 
while true do
    for _,Player in pairs(game.Players:GetPlayers()) do
		PrestigeODS:SetAsync(Player.Name, Player.leaderstats.YOURDATA.Value)
    end
    for _,v in pairs(game.Workspace.GlobalLeaderboard2.lbGUI.Holder:GetChildren()) do
        if v.Name == "lbFrame" then
            v:Destroy()
        end
    end
    Handler()
    wait(20) --Time until Refresh
end

1 Like

It should be data.value
(lowercase v)

This is the same thing that i have, you just have the system for include a leaderboard,
my problem is that without or with this doesn’t work, and i don’t know why, my data seems to be correct…

Thanks despite all

2 Likes

It doesn’t means a lot, but i change it, and it change nothing…

1 Like

Here is some news.

I tried assigning test values to my DataStores and it managed to get a value out.
The problem comes from my DataStores but I don’t really understand why.
I’m using my DataStores for the leaderboard, the value is stored in the player thanks to another script.

Here is the script for the leaderboard (with the test values):

-- << 	Services >> --
local DataStoreService = game:GetService("DataStoreService")
-- << 	Variables >> --
local data_Donations = DataStoreService:GetOrderedDataStore("DonationsScore")
-- << Functions >> --
data_Donations:SetAsync("Mars", 19) --Temporary
data_Donations:SetAsync("Marss", 1058)  --Temporary
data_Donations:SetAsync("Marssss", 234)  --Temporary

local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = data_Donations:GetSortedAsync(false, 10)
		local topTEN = Data:GetCurrentPage()
		
		for rank, data in ipairs(topTEN) do
			local name = data.key
			local score = data.Value
			print(name.." is ranked #"..rank.." with "..score.."score")
		end
	end)
	
	if not success then
		print(errorMessage)
	end
end
-- << Connect >> --
while true do
	updateLeaderboard()
	print("Updated!")
	
	wait(10) -- Temp
end

And here is the script that manages the dataStores:

-- << 	Services >> --
local DataStoreService = game:GetService("DataStoreService")
-- << 	Variables >> --
local data_MyOtherScores = DataStoreService:GetDataStore("MyOtherScores")
local data_Donations = DataStoreService:GetDataStore("DonationsScore") -- HERE IS THE DATA USED FOR THE LEADERBOARD
local data_BetterTime = DataStoreService:GetDataStore("BetterTimeScore")
-- << Functions >> --
local function saveData(player)
	local dataToSave = {
		player.DataRepertory.LastTime.Value;
		player.DataRepertory.WorseTime.Value
	}
	local donationsScore = player.DataRepertory.DonationScore.Value
	local betterTime = player.DataRepertory.BetterTime.Value
	
	local success, err = pcall(function()
		data_MyOtherScores:SetAsync(player.UserId, dataToSave)
		data_BetterTime:SetAsync(player.UserId, betterTime)
		data_Donations:SetAsync(player.UserId, donationsScore)
	end)
	
	if success then
		print("Data has been saved !")
	else
		print("Data hasn't been saved !")
		warn(err)
	end
end
-- << Connect >> --
game.Players.PlayerAdded:Connect(function (player)
	-----Creating a folder for the data into the player
	local dataFolder = Instance.new("Folder")
	dataFolder.Name = "DataRepertory"
	dataFolder.Parent = player
	
	local BetterTime = Instance.new("NumberValue")
	BetterTime.Name = "BetterTime"
	BetterTime.Parent = dataFolder
	
	local LastTime = Instance.new("NumberValue")
	LastTime.Name = "LastTime"
	LastTime.Parent = dataFolder

	local WorseTime = Instance.new("NumberValue")
	WorseTime.Name = "WorseTime"
	WorseTime.Parent = dataFolder
	
	local DonationScore = Instance.new("IntValue")
	DonationScore.Name = "DonationScore"
	DonationScore.Parent = dataFolder
	
	local dataScores
	local dataDonations
	local dataBetterTime
	local success, err = pcall(function ()
		dataScores = data_MyOtherScores:GetAsync(player.UserId)
		dataDonations = data_Donations:GetAsync(player.UserId)
		dataBetterTime =  data_BetterTime:GetAsync(player.UserId)
	end)
	wait(2) -- wait ajouté ici pour que le Menu fassent le changement des valeurs au début (sinon l'event .Changed n'est pas appellé au début)
	if success then
		LastTime.Value = dataScores[1]
		WorseTime.Value = dataScores[2]
		BetterTime.Value = dataBetterTime
		DonationScore.Value = dataDonations
	else
		print("The player has not data !")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function ()
		saveData(player)
	end)
	
	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
		warn(err)
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
			warn(err)
		end
	end
end)

If somebody has a idea of why it don’t work, i just started using datastores.

I will close this topic and create a new one, because i know where the problems comes from and it will me more simple to get an anwser.