Leaderboard script does not work

I’m trying to do a leaderboard script, but it doesn’t work, but the save system does.

task.spawn(function()
	local DonationODS = DataStoreService:GetOrderedDataStore("DonationData")
	local pages = DonationODS:GetSortedAsync(false, dataTable.TopDonatorsAmount)
	local topDonators = pages:GetCurrentPage()
	
	for index, data in ipairs(topDonators) do
		local success, name = pcall(Players, Players.GetNameFromUserIdAsync, data.key)
		if success then
			table.insert(dataTable.TopDonators, {
				Username = name,
				AmountDonated = data.value.AmountDonated
			})
		end
	end
end)
2 Likes

When I print the topDonators variable it is empty, but it shouldn’t be because I saved data.

1 Like

Are there any errors? If there are none then can I see the whole script?

1 Like

There are no errors in the output and this is the complete script:

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local DonationData = DataStoreService:GetDataStore("DonationData")
local RunService = game:GetService("RunService")
local Products = require(game.ReplicatedStorage.Products).Products

local dataDonation = {}

local dataTable = {
	TopDonatorsAmount = 10,
	TopDonators = {}
}

task.spawn(function()
	local DonationODS = DataStoreService:GetOrderedDataStore("DonationData")
	local pages = DonationODS:GetSortedAsync(false, dataTable.TopDonatorsAmount)
	local topDonators = pages:GetCurrentPage()
	
	for index, data in ipairs(topDonators) do
		local success, name = pcall(Players, Players.GetNameFromUserIdAsync, data.key)
		if success then
			table.insert(dataTable.TopDonators, {
				Username = name,
				AmountDonated = data.value.AmountDonated
			})
		end
	end
end)

Players.PlayerAdded:Connect(function(player)
	local success, data = pcall(function()
		return DonationData:GetAsync(player.UserId)
	end)

	if success and data then
		dataDonation[player.UserId] = data
	elseif success and not data then
		dataDonation[player.UserId] = {
			AmountDonated = 0
		}
	elseif not success then
		warn("An error occured while loading the player data")
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local data = dataDonation[player.UserId]
	if data then
		local success, err = pcall(function()
			DonationData:UpdateAsync(player.UserId, function(oldData)
				return data
			end)
		end)
		if not success then
			warn(err)
		end
	end
end)

game:BindToClose(function()
	if RunService:IsStudio() then return end
	for _, player in pairs(Players:GetPlayers()) do
		local data = dataDonation[player.UserId]
		if data then
			local success, err = pcall(function()
				DonationData:UpdateAsync(player.UserId, function(oldData)
					return data
				end)
			end)
			if not success then
				warn(err)
			end
		end
	end
end)

MarketplaceService.ProcessReceipt = function(receiptInfo)
	for _, product in pairs(Products) do
		if product.ProductID == receiptInfo.ProductId then
			local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
			if player then
				dataDonation[player.UserId].AmountDonated += product.Price
				game.ReplicatedStorage.PurchaseComplete:Fire(player, product.Price)
				return Enum.ProductPurchaseDecision.PurchaseGranted
			else
				return Enum.ProductPurchaseDecision.NotProcessedYet
			end
		end
	end
end

game.ReplicatedStorage.GetLeadeboardInfo.OnServerInvoke = function(player)
	return {
		AmountDonated = dataDonation[player.UserId].AmountDonated,
		TopDonatorsAmount = dataTable.TopDonatorsAmount,
		TopDonators = dataTable.TopDonators
	}
end
1 Like

Ok so

Wrap this in a pcall ^

Are you sure that there are data stored in that DataStore?

1 Like

The main error I see is with the pcall
You’re using pcall(Players, Players.GetNameFromUserIdAsync, data.key)
When it should be: pcall(Players.GetNameFromUserIdAsync, Players, data.key)

The reason for this is because with a . function, you pass the actual object which you perform the function on (commonly known as self)

So: ObjectA.DoSomething(ObjectA) is the same as ObjectA:DoSomething()
Thats why in pcall’s you pass the object for the function to run otherwise it will error

1 Like

No errors appear in the output and success is true.

Yes there is data stored in that datastore.

1 Like

Hi, thanks for your help I added a print inside the loop and it never prints. and the topDonators variable is empty.

1 Like

Mind showing the script?

1 Like

Hello here is the complete script:

1 Like

You didn’t change the pcall code? Also I’ve noticed GetLeadeboardInfo might be spelt wrong.

1 Like

Hi, I tried it but it still didn’t work.

1 Like

It may not work because there is only one key currently saved?

1 Like

Hm, not sure

1 Like

Does it work if I am testing it in roblox studio?

1 Like

Well you need to enabled studio API for that to work

1 Like

I checked it and it is enabled.

1 Like

Hm, then I don’t know, not too experienced with data-stores myself

1 Like

In your game:BindToClose function, you made it so it won’t save in studio. Delete the block of if statement. Also, copy and paste the code in there and put it in a game.Players.PlayerRemoving:Connect(function(player)
This way it saves when a player is leaving because sometimes the game closes after it removes the players. If it worked it would be appreciated if you set this as solution. :slight_smile:

1 Like

Hello, the problem is not in the save part.

The problem is here:

The problem is that when I print the topDonators variable it is empty, but I saved a key so I’m not sure why that happens.

1 Like