How to make top support surface gui?

so i am making a donation game, and i already made a surface gui where players can click on one of the support buttons and buy devproducts.
well now i want to make another surface gui, but it displays the top ten sponsors, so the people who have given the most robux. i tried to make some code but instead of it putting something on the board nothing happens…
my code:

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local TopsponsorboardPart = game.Workspace.Topsponsorboard:FindFirstChild("Part1")
local Donations = {} -- Table to keep track of donations

-- Function to update the SurfaceGui on the Topsponsorboard part
local function updateSurfaceGui()
    if not TopsponsorboardPart then return end
    local surfaceGui = TopsponsorboardPart:FindFirstChildOfClass("SurfaceGui")
    if not surfaceGui then
        surfaceGui = Instance.new("SurfaceGui", TopsponsorboardPart)
        surfaceGui.Name = "DonationListGui"
        surfaceGui.Adornee = TopsponsorboardPart
        surfaceGui.Face = Enum.NormalId.Front
    end
    
    local frame = surfaceGui:FindFirstChild("Frame")
    if not frame then
        frame = Instance.new("Frame", surfaceGui)
        frame.Size = UDim2.new(1, 0, 1, 0)
    end
    
    frame:ClearAllChildren() -- Clear previous list
    
    local uiListLayout = Instance.new("UIListLayout", frame)
    uiListLayout.Padding = UDim.new(0, 5)
    uiListLayout.SortOrder = Enum.SortOrder.LayoutOrder
    
    -- Sort the donations table by amount, descending
    local sortedDonations = {}
    for name, amount in pairs(Donations) do
        table.insert(sortedDonations, {name = name, amount = amount})
    end
    table.sort(sortedDonations, function(a, b) return a.amount > b.amount end)
    
    -- Display top 10 donations
    for i = 1, math.min(#sortedDonations, 10) do
        local donation = sortedDonations[i]
        local textLabel = Instance.new("TextLabel", frame)
        textLabel.Size = UDim2.new(1, 0, 0, 20)
        textLabel.Text = donation.name .. ": " .. donation.amount .. " Robux"
        textLabel.TextColor3 = Color3.new(1, 1, 1)
        textLabel.BackgroundTransparency = 1
	end
	
	for i = 1, math.min(#sortedDonations, 10) do
		local donation = sortedDonations[i]
		local textLabel = Instance.new("TextLabel", frame)
		textLabel.Text = donation.name .. ": $" .. donation.amount
		textLabel.Size = UDim2.new(1, 0, 0, 50)
		textLabel.LayoutOrder = i
	end

end

-- Function to handle developer product purchases
local function onProductPurchase(player, productId, purchaseId)
    -- Assuming you have a way to determine the amount of Robux for each productId
    local productAmount = {
        -- Example: [productId] = amount
		[1837109332] = 5, -- Replace 12345678 with actual product IDs and amounts
        -- Add more products as needed
    }
    
    local amount = productAmount[productId]
    if amount then
        if not Donations[player.Name] then
            Donations[player.Name] = 0
        end
        Donations[player.Name] = Donations[player.Name] + amount
        updateSurfaceGui()
    end
end

MarketplaceService.ProcessReceipt = function(receiptInfo)
    local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
    if player then
        onProductPurchase(player, receiptInfo.ProductId, receiptInfo.PurchaseId)
        return Enum.ProductPurchaseDecision.PurchaseGranted
    else
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end
end

-- Initial update in case of server restart or script update
updateSurfaceGui()

this is a script in serverScriptService

2 Likes

Still need help on this, lol. I really dont know how to fix this. Please help, i am new to programming.

1 Like

It doesn’t look like you’re updating the Donations list outside of onProductPurchase(). Is “Donations” only stored on the individual server? Or are you using DataStoreService to store it? DataStoreService | Documentation - Roblox Creator Hub

1 Like

I believe that it is just on the individual server, but i could be wrong, as you can tell i have no idea what i am talking about.

This is also the only script i am using for updating the top board, so if its not on here then its not on the board.

1 Like

So, you’re going to need to gather data on server start before updating the leaderboard, otherwise “Donations” is going to stay empty until someone.

I do suggest storing the data on datastore service as well so that the data stays in case the server restarts and it will be the same throughout multiple servers.
I definitely recommend reading through this doc to learn more about them if you want to make a leaderboard that keeps data across servers Data Stores | Documentation - Roblox Creator Hub.
Since you’re making a leaderboard, I’ll definitely read up and try to implement Ordered Data Stores, which is in the document i linked above.

Otherwise, your UI scripting looks okay, its just Donations is empty on server start so its not creating any textlabels.

1 Like

What exactly would i fill donations with?

1 Like

So if you use DataStores you will get the lifetime top 10 donators and how much they’ve donated. Otherwise, it would be empty on server start since no one will have donated in that server yet.

From what i can tell from this for loop:

You’re filling Donations with a player’s name as the indice and the amount donated as the value like:

Donations = {
    ["Player1"] = 200,
    ["Player2"] = 100,
    ...
}

But since you aren’t using any way of keeping data stored between servers, when a new server starts, Donations is going to be empty.

1 Like