Leaderboard not updating / error

I want the leaderboard to update every 10 seconds with new values (if any)

The issue is that it gives me an error saying it won’t update. I have no clue what the error/bug is.

I have tried adding error messages to tell me where, tried reading the code but everything looked fine.

Code:

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

local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = DonationsLeaderboard:GetSortedAsync(false, 5)
		local DonationsPage = Data:GetCurrentPage()
		for Rank, data in ipairs(DonationsPage) do
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = userName
			local Donations = data.value
			local isOnLeaderboard = false
			for i, v in pairs(game.Workspace.Leaderboard.SurfaceGui.ScrollingFrame:GetChildren()) do
				if v.Player.Text == Name then
					isOnLeaderboard = true
					break
				end
			end
			

			if Donations > 0 and isOnLeaderboard == false then
				local newLbFrame = game.ReplicatedStorage:WaitForChild("Sample"):Clone()
				newLbFrame.Player.Text = Name
				newLbFrame.Donations.Text = Donations
				newLbFrame.Rank.Text = "#"..Rank
				newLbFrame.Position = UDim2.new(0, 0, newLbFrame.Position.Y.Scale + (.08 * #game.Workspace.Leaderboard.SurfaceGui.ScrollingFrame:GetChildren()), 0)
				newLbFrame.Parent = game.Workspace.Leaderboard.SurfaceGui.ScrollingFrame
			end
		end
	end)
	
	if not success then
		print(errorMessage)
	end
end

while true do

	for _, player in pairs(game.Players:GetPlayers()) do
		DonationsLeaderboard:SetAsync(player.UserId, player.leaderstats.Donations.Value)
	end

	for _, frame in pairs(game.Workspace.Leaderboard.SurfaceGui.ScrollingFrame:GetChildren()) do
		frame:Destroy()
	end
	
	if not updateLeaderboard() then
		print("Updating leaderboard error")
	else
		updateLeaderboard()
		print("Updated leaderboard")
	end
	

	wait(10)
end

Anybody know?

  • I’m using a normal script and it is located in ServerScriptService

You don’t need to use it. Also why didn’t you use the function before these lines? I meant you should use the function after for loops.

updateLeaderboard() 

Your while loop

while true do

    for _, player in pairs(game.Players:GetPlayers()) do
	    DonationsLeaderboard:SetAsync(player.UserId, player.leaderstats.Donations.Value)
    end

    for _, frame in pairs(game.Workspace.Leaderboard.SurfaceGui.ScrollingFrame:GetChildren()) do
	    frame:Destroy()
    end

    updateLeaderboard() -- Use the function you defined it above
    print("Successfully saved!")

    wait(10)
end

That script also didn’t work. I tried the way you are showing me.

Do you have any error in the output?

It says “Saved” but it isn’t showing on my leaderboard.

Is your donation value over 0?

1 Like

Wait! It is showing. I changed my value during testing but never exited out of the client. It does work.

1 Like