Why doesn't this ordered DataStore work with my leaderboard?

I have just watched a YouTube tutorial on how to make a leaderboard, and followed what it said but obviously made some changes to work with my own DataStore. I currently have a DataStore (Which works correctly) called ‘DonatedDataStore’ and want to use this DataStore to order donations (Not planning to use the idea, just using it to learn about leaderboards + datastores), however, the code that I have used doesn’t actually do anything and doesn’t show errors. Throughout the code, I have put markers in which I have tested things and it hasn’t worked. If someone could look through it and see if I HAVE made any mistakes (Like I said, I’m new to this), that would be great. I’ll post the code below.

local DataStoreService = game:GetService("DataStoreService")
local DonatedDataStore = DataStoreService:GetOrderedDataStore("DonatedDataStore")

local function UpdateLeaderboard() -- i have a feeling that it is something to do with accessing the data in the datastore
	local success, errormessage = pcall(function()
		local Data = DonatedDataStore:GetSortedAsync(false, 5)
		local DonationPage = Data:GetCurrentPage()
		for rank, Data in ipairs(DonationPage) do
			print(rank) -- also doesnt print anything
			print("HellO")
			local Username = game.Players:GetNameFromUserIdAsync(tonumber(Data.Key)) -- one thing I wasn't sure about
			print(Username)
			local Donated = Data.Value
			local isOnLeaderboard = false
			for i,v in pairs(game.Workspace.DonationBoard.DonationLeaderboard.Background.ScrollingFrame:GetChildren()) do
				if v.Player.Text == Username then
					isOnLeaderboard = true
					break
				end
			end
			
			if Donated and isOnLeaderboard == false then
				-- nothing happens here, new frame isn't created
				local newLBFrame = game.ReplicatedStorage:WaitForChild("DonationExample"):Clone()
				newLBFrame.Player.Text = Username
				newLBFrame.Amount.Text = Donated
				newLBFrame.Rank = "#"..rank
				newLBFrame.Position = UDim2.new(0, 0, newLBFrame.Position.Y.Scale + (0.08 * game.Workspace.DonationBoard.DonationLeaderboard.Background.ScrollingFrame:GetChildren()), 0)
				newLBFrame.Parent = game.Workspace.DonationBoard.DonationLeaderboard.Background.ScrollingFrame
			end
		end
	end)
	if not success then
		warn(errormessage)
	end
end

while true do
	for _, players in pairs(game.Players:GetChildren()) do
		DonatedDataStore:SetAsync(player.UserId, -- he used a value from leaderstats here, but I'm not using leaderstats)
	end
	
	for _, frame in pairs(game.Workspace.DonationBoard.DonationLeaderboard.Background.ScrollingFrame:GetChildren()) do
		frame:Destroy()
	end
	UpdateLeaderboard()
	print("Updated")
	wait(10) 
end

In-case you wanted to see the video reference, it was made by TheDevKing, and here is the link.

1 Like

Hello there! From what I’m currently seeing is that you are reffering to your Key and Value data as

game.Players:GetNameFromUserIdAsync(tonumber(Data.Key))
local Donated = Data.Value

Please make sure you refer the Key and Value in lowercase letters, these lines of code are Case Sensitive, I just tested this out with my current leaderboard and experienced the same issue. The following lines of code are the fixed methods of referring to the key and data.

game.Players:GetNameFromUserIdAsync(tonumber(Data.key)) --Changed Key to key
local Donated = Data.value --Changed Value to value

As for other errors, it would be really helpful to provide some of errors you are seeing in the developer console panel, as it will tell us the specific error and which specific line of code these errors are coming from and will help us to fully debug the code! Also please make sure you are saving the data using GetOrderedDataStore when you are saving players data, as GetDataStore is a complete seperate storage of data, which won’t work when you try to refer data that is saved as GetDataStore as GetOrderedDataStore. Hope this helps! :slightly_smiling_face:

1 Like

I don’t really think this has anything to do with it. Key and Value can be deprecated, so it really wouldn’t make a difference in this case.

As I am currently seeing it, they are not deprecated, I use them all the time in my current leaderboards. It is the method of referring to which userid the data is being saved to, as well as the value that is stored in the datastore under the player. OrderedDataStore | Documentation - Roblox Creator Hub

Also here is another devforum post (that I used back then) that helped me to work my ways around learning how to make a leaderboard! How to make a simple global leaderboard

Here are some things.

There is a different way of getting the Username. You can do

local Username = data.Key

Also, there was one other thing in the while true do loop. You referenced the player id as player.UserId when it should’ve been players.UserId with an “s”

data.key returns the UserID of the datastore, not the name. He is currently correctly getting the username with using the line:

local Username = game.Players:GetNameFromUserIdAsync(tonumber(Data.key)) --Changed Key to lowercase.

This correctly gets the players username from UserID. This can be seen in the developer API OrderedDataStore | Documentation - Roblox Creator Hub.

You are correct where there is a misplaced “s” in the while true do loop. Here is the correction line of code:

for _, players in pairs(game.Players:GetChildren()) do
	DonatedDataStore:SetAsync(players.UserId, statsvalue)
end
1 Like

Surprisingly, this was the solution. The whole time the leaderboard was actually working, it just wasn’t getting any information because I was saving the old data into an Unordered data store, meaning it technically didn’t have any information. I didn’t realise that these were two different types of data stores, so my original saving code in another script has been changed and it is now sorted.

2 Likes