Global leaderboard won't update nor show anything, need help!

Hey everyone, once again I’ve been trying to solve this issue for quite a while and I give up and I need to ask for help because even roblox assistant doesn’t work anymore here.
So basically my issue is: at first it worked kinda because it showed every player but the values were outdated, then I changed it so it only shows from cash datastore and after some time it stopped working, now it doesn’t show any errors and doesn’t work.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetOrderedDataStore("cashds_1")

local surfaceGui = script.Parent
local sample = script:WaitForChild("Sample")
local sf = surfaceGui:WaitForChild("ScrollingFrame")
local uiListLayout = sf:FindFirstChildOfClass("UIListLayout") or Instance.new("UIListLayout")
uiListLayout.SortOrder = Enum.SortOrder.LayoutOrder
uiListLayout.Parent = sf


local existingLabels = {}  -- Use a dictionary to keep track of existing labels

local function getPlayerCash(userId)
	print(userId)
	local key = tostring(userId)
	local success, cash = pcall(function()
		local data = dataStore:GetAsync(key)
		print(data)
		if data and data.Cash then
			return data.Cash
		else
			return 0
		end
	end)
	if success then
		return cash
	else
		warn("[Global Leaderboard] Error getting cash for player", userId)
		return 0
	end
end


local function getTopPlayers()
	local success, pages = pcall(function()
		return dataStore:GetSortedAsync(false, 100, 1, 10e30)
	end)

	if not success then
		warn("[Global Leaderboard] Error getting pages:", pages)
		return nil
	end

	return pages:GetCurrentPage()
end


local function updateLeaderboard()
	print("[Global Leaderboard] Updating leaderboard at", os.date("%Y-%m-%d %H:%M:%S"))

	local top = getTopPlayers()

	if not top then
		return
	end

	-- Clear existing labels
	for _, label in pairs(existingLabels) do
		label:Destroy()
	end

	existingLabels = {}  -- Reset the dictionary

	for rank, entry in ipairs(top) do
		local userId = entry.key

		print("[Global Leaderboard] Looking up player for userId:", userId)

		local player = Players:GetPlayerByUserId(userId)

		local color = Color3.new(1, 1, 1)

		if rank == 1 then
			color = Color3.new(1, 1, 0)
		elseif rank == 2 then
			color = Color3.new(0.9, 0.9, 0.9)
		elseif rank == 3 then
			color = Color3.new(166, 112, 0)
		end

		local new = sample:Clone()
		new.Name = userId
		new.LayoutOrder = rank
		new.Parent = sf

		local playerNameLabel = new:WaitForChild("playerName")
		local rankLabel = new:WaitForChild("rank")
		local valueLabel = new:WaitForChild("value")

		if player then
			playerNameLabel.Text = player.Name
		else
			local success, username = pcall(function()
				return Players:GetNameFromUserIdAsync(userId)
			end)

			if success then
				playerNameLabel.Text = username
			else
				warn("[Global Leaderboard] Unable to get username for userId", userId)
				playerNameLabel.Text = "Unknown"
			end
		end

		local cash = getPlayerCash(userId)
		entry.value = cash

		rankLabel.Text = "#" .. rank
		rankLabel.TextColor3 = color
		valueLabel.Text = cash
		valueLabel.TextColor3 = color
		playerNameLabel.TextColor3 = color

		print("[Global Leaderboard] Player:", player and player.Name or "Not in game", "Cash:", cash)
		print("[Global Leaderboard] UI updated for player", userId, "with cash value", cash, "at", os.date("%Y-%m-%d %H:%M:%S"))

		existingLabels[userId] = new  -- Add the new label to the dictionary
	end
end


local function onPlayerChanged(player)
	local success, result = pcall(function()
		updateLeaderboard()
	end)

	if not success then
		warn("[Global Leaderboard] Error updating leaderboard:", result)
	end

	local playerGui = player:FindFirstChildOfClass("PlayerGui")
	if playerGui then
		local coinLeaderboardGui = playerGui:FindFirstChild("coinLeaderboard")
		if coinLeaderboardGui then
			local surfaceGui = coinLeaderboardGui:FindFirstChild("SurfaceGui")
			if surfaceGui then
				local sf = surfaceGui:FindFirstChild("ScrollingFrame")
				if sf then
					local success, result = pcall(function()
						updateLeaderboard()
					end)

					if not success then
						warn("[Global Leaderboard] Error updating leaderboard:", result)
					end
				end
			end
		end
	end
end

Players.PlayerAdded:Connect(onPlayerChanged)
Players.PlayerRemoving:Connect(onPlayerChanged)

-- Function to automatically update leaderboard every 5 seconds
local function autoUpdateLeaderboard()
	while true do
		local success, result = pcall(function()
			updateLeaderboard()
		end)

		if not success then
			warn("[Global Leaderboard] Error updating leaderboard:", result)
		end
		wait(5) -- Update every 5 seconds
	end
end

-- Start the automatic update loop
autoUpdateLeaderboard()

Any help is welcomed because it hurts my head fixing it. I literally spent hours and days with little but no result.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetOrderedDataStore("cashds_1")

local surfaceGui = script.Parent
local sample = script:WaitForChild("Sample")
local sf = surfaceGui:WaitForChild("ScrollingFrame")
local uiListLayout = sf:FindFirstChildOfClass("UIListLayout") or Instance.new("UIListLayout")
uiListLayout.SortOrder = Enum.SortOrder.LayoutOrder
uiListLayout.Parent = sf

local existingLabels = {}

local function getPlayerCash(userId)
    local key = tostring(userId)
    local success, data = pcall(function()
        return dataStore:GetAsync(key)
    end)
    if success and data then
        return data.Cash or 0
    else
        warn("[Global Leaderboard] Error getting cash for player", userId)
        return 0
    end
end

local function getTopPlayers()
    local success, pages = pcall(function()
        return dataStore:GetSortedAsync(false, 100, 1, 10e30)
    end)
    if not success then
        warn("[Global Leaderboard] Error getting pages:", pages)
        return nil
    end
    return pages:GetCurrentPage()
end

local function updateLeaderboard()
    print("[Global Leaderboard] Updating leaderboard at", os.date("%Y-%m-%d %H:%M:%S"))
    local top = getTopPlayers()
    if not top then
        return
    end
    for _, label in pairs(existingLabels) do
        label:Destroy()
    end
    existingLabels = {}
    for rank, entry in ipairs(top) do
        local userId = entry.key
        local cash = getPlayerCash(userId)
        local new = sample:Clone()
        new.Name = userId
        new.LayoutOrder = rank
        new.Parent = sf
        local playerNameLabel = new:WaitForChild("playerName")
        local rankLabel = new:WaitForChild("rank")
        local valueLabel = new:WaitForChild("value")
        local player = Players:GetPlayerByUserId(userId)
        playerNameLabel.Text = player and player.Name or "Unknown"
        rankLabel.Text = "#" .. rank
        valueLabel.Text = cash
        existingLabels[userId] = new
    end
end

local function onPlayerChanged(player)
    local success, result = pcall(function()
        updateLeaderboard()
    end)
    if not success then
        warn("[Global Leaderboard] Error updating leaderboard:", result)
    end
end

Players.PlayerAdded:Connect(onPlayerChanged)
Players.PlayerRemoving:Connect(onPlayerChanged)

local function autoUpdateLeaderboard()
    while true do
        local success, result = pcall(function()
            updateLeaderboard()
        end)
        if not success then
            warn("[Global Leaderboard] Error updating leaderboard:", result)
        end
        wait(5)
    end
end

autoUpdateLeaderboard()

try this script (i am a new scripter so i dont know all)

It could be one of two things:

  1. You are sending to many requests to the DataStore.
  2. You haven’t actually written any entries to the store (not updating them).

Try sending less requests, for instance you don’t need to use Store:GetAsync() to get the player’s data if you’ve already written it into the ordered store. It just provides another area where the store can error internally.

--------EXAMPLE--------
local ds = game:GetService("DataStoreService")
local players = game:GetService("Players")

--we need to save a player's data before we read it.
local function save(player)
    local store = ds:GetOrderedDataStore("Cash")
    local success, result = pcall(store.SetAsync, store, player.UserId, player.leaderstats.Money.Value)
end

--now, we can read it!
local function update()
    local store = ds:GetOrderedDataStore("Cash")
    local pages = store:GetSortedAsync(false, 100)

    for rank, saveData in ipairs(pages:GetCurrentPage()) do
        local idKey = page[rank]
        local userId = tonumber(idKey.key)
        local user = players:GetNameFromUserIdAsync(userId)
        if user then
            print(`#{rank} place is {user} with {saveData.value} Money!`)
        end
    end
end

local success, result = pcall(update)

Uh! I have a problem now. I’ve tried your fix and it kinda worked but umm I joined my game, it didn’t show a thing. Nothing, the leaderboard was just empty. Then, I left the game added prints to see which part doesn’t work, I joined and magically it started working (idk why honestly because I only added prints) and uh I saw only myself on the leaderboard and I’m not sure why there aren’t other players yet.

// edit: I didn’t see any output errors, now it “works” everytime but still only as you see on the screen so only for me (like locally? or idk) and also it doesn’t update when it refreshes so like when I sell, it only updates when I leave and join back.

// edit 2: it works for other people, however the only issue right now seems to be it doesn’t auto-update but only when the server is like rebuilt so when all players leave and join back. And idk why but first 3 places aren’t colored anymore lol
image

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.