How to refresh a global leaderboard

I made a global leaderboard that displays the player’s points. However, the problem is that the leaderboard only refreshes per game, aka it will always be a game behind. I want it to preferbaly refresh every minute but I am not quite sure how to do that. Here is my serverside script:

local DataStorePages = PointsDataStore:GetSortedAsync(false, 12, 1, 10e30)
local top = DataStorePages:GetCurrentPage()

for _, v in ipairs(top) do
	local points = v.value
	local username = Players:GetNameFromUserIdAsync(v.key)
	table.insert(data, {username, points})
end

game.ReplicatedStorage.GlobalLeaderboard:FireAllClients(data) --client side gui

You could wrap it in a while loop and wait a certain amount of time between each loop.

--while loop
for _, v in ipairs(top) do
	local points = v.value
	local username = Players:GetNameFromUserIdAsync(v.key)
	table.insert(data, {username, points})
end

game.ReplicatedStorage.GlobalLeaderboard:FireAllClients(data)
--wait(amountOfTime)
--end while loop

I tried this but all it would do is print the same data over and over again. The actual values of the data don’t get updated.

Wouldn’t you have to renew the values of data before firing to the client? if not it will just fire the same value it save from the beginning. Also idk how to format thats why i made comments instead lmao

while true do
for _, v in ipairs(top) do
	local points = v.value
	local username = Players:GetNameFromUserIdAsync(v.key)
	table.insert(data, {username, points})
end

game.ReplicatedStorage.GlobalLeaderboard:FireAllClients(data)
wait(60)
end

Try doing this boot (Sorry about formatting)

Exactly as Ahmed said, you’d need to refresh your values. Could we see the script handling the data RemoteEvent?

local one = script.Parent.one	
local two = script.Parent.two
local three = script.Parent.three
local four = script.Parent.four
local five = script.Parent.five
local six = script.Parent.six
local seven = script.Parent.seven	
local eight = script.Parent.eight
local nine = script.Parent.nine	
local ten = script.Parent.ten
local eleven = script.Parent.eleven
local twelve = script.Parent.twelve
local Eone = script.Parent.Eone	
local Etwo = script.Parent.Etwo
local Ethree = script.Parent.Ethree
local Efour = script.Parent.Efour
local Efive = script.Parent.Efive
local Esix = script.Parent.Esix
local Eseven = script.Parent.Eseven	
local Eeight = script.Parent.Eeight
local Enine = script.Parent.Enine	
local Eten = script.Parent.Eten
local Eeleven = script.Parent.Eeleven
local Etwelve = script.Parent.Etwelve

game.ReplicatedStorage.GlobalLeaderboard.OnClientEvent:Connect(function(array)
	one.Text = array[1][1]
	Eone.Text = array[1][2]
	two.Text = array[2][1]
	Etwo.Text = array[2][2]
	three.Text = array[3][1]
	Ethree.Text = array[3][2]
	four.Text = array[4][1]
	Efour.Text = array[4][2]
	five.Text = array[5][1]
	Efive.Text = array[5][2]
	six.Text = array [6][1]
	Esix.Text = array [6][2]
	seven.Text = array [7][1]
	Eseven.Text = array [7][2]
	eight.Text = array [8][1]
	Eeight.Text = array [8][2]
	nine.Text = array [9][1]
	Enine.Text = array [9][2]
	ten.Text = array [10][1]
	Eten.Text = array [10][2]
	eleven.Text = array [11][1]
	Eeleven.Text = array [11][2]
	twelve.Text = array [12][1]
	Etwelve.Text = array [12][2]
end)

oh no.

let’s start with a bit of… code refactoring.
We can turn your localscript more readable using a combination of arrays and for loops

local RS = game:GetService("ReplicatedStorage")

local parent = script.Parent
local numArray = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"}

local function setText(array)
    for index, str in pairs(numArray) do
        parent:FindFirstChild(str).Text = array[index][1]
        parent:FindFirstChild("E" .. str).Text = array[index][2]
    end
end

RS.GlobalLeaderboard.OnClientEvent:Connect(setText)

Now that we shortened about 50 lines of code into 12, let’s move on with your issue. As @ahmedluai22 has said before, you also need to check the user data and update the table before sending out the signal, so your serverscript would indeed look something like this:

 while true do
    for _, v in ipairs(top) do
        local points = v.value
        local username = Players:GetNameFromUserIdAsync(v.key)
        table.insert(data, {username, points})
    end

    game.ReplicatedStorage.GlobalLeaderboard:FireAllClients(data)
    wait(60)
end

Hope i helped!

1 Like

Using this code, I get a leaderboard that looks something like this:

Player Name Points
Player A 100
Player B 50
Player A 100
Player B 50