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)
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
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
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.