Why is my GUI leaderboard sometimes not loading?

Hello, I’m making a Leaderboard GUI for my game, and although it works for the main part, I’ve ran into some issues loading it. It seems to mostly work in studio (although sometimes I’m looking at an empty GUI) but in game it seems to not work at all. This is the server-side:

local DSS = game:GetService("DataStoreService")
local HighscoreStore = DSS:GetOrderedDataStore("HighscoreStore")
local BadgeService = game:GetService("BadgeService")
local function GetHighestRank()
	local isAscending = false
	local pageSize = 50
	local pages = HighscoreStore:GetSortedAsync(true, pageSize, 1)
	local topFifty = pages:GetCurrentPage()
	for rank, data in ipairs(topFifty) do
		local name = data.key
		local points = data.value
		game.ReplicatedStorage.Rank:FireAllClients(rank, name, points)
		for i, v in pairs(game.Players:GetPlayers()) do
			if v.Name == game.Players:GetNameFromUserIdAsync(name) then
				if not BadgeService:UserHasBadgeAsync(v.UserId, 2147668754) then
					BadgeService:AwardBadge(v.UserId, 2147668754)
				end
			end 
		end
	end
end
GetHighestRank()

This basically fetches the top 50 and fires a remote event for the client to pick up the list for every index. This is the Client-side:

game.ReplicatedStorage.Rank.OnClientEvent:Connect(function(rank, name, points)
	script.Temp:FindFirstChild("Position").Text = "["..rank.."]"
	script.Temp.Username.Text = game.Players:GetNameFromUserIdAsync(name)
	script.Temp.Value.Text = points
	local tempCopy = script.Temp:Clone()
	tempCopy.Parent = script.Parent
	tempCopy.Name = rank
	tempCopy.Visible = true
end)

Basically all this does is take a template, and clone it for every index. When it actually loads, this is how it looks.

Can anyone please help me and show me why it refuses to load in-game and loads sometimes in studio?

1 Like

The issue might be related to the timing of when the client-side code is executed. Since the leaderboard GUI is dependent on receiving data from the server, it’s important to ensure that the client-side code runs after the server has sent the data.

One possible solution is to use a RemoteFunction instead of a RemoteEvent to retrieve the leaderboard data from the server. This way, the client can request the data when needed, and the server can respond with the leaderboard information. Here’s an updated version of the code that implements this approach:

Server-side:

local DSS = game:GetService("DataStoreService")
local HighscoreStore = DSS:GetOrderedDataStore("HighscoreStore")
local BadgeService = game:GetService("BadgeService")

local function GetHighestRank()
	local isAscending = false
	local pageSize = 50
	local pages = HighscoreStore:GetSortedAsync(true, pageSize, 1)
	local topFifty = pages:GetCurrentPage()

	local leaderboardData = {}
	for rank, data in ipairs(topFifty) do
		local name = data.key
		local points = data.value
		table.insert(leaderboardData, {rank = rank, name = name, points = points})
		
		-- Check for badge awarding
		for _, player in ipairs(game.Players:GetPlayers()) do
			if player.Name == game.Players:GetNameFromUserIdAsync(name) then
				if not BadgeService:UserHasBadgeAsync(player.UserId, 2147668754) then
					BadgeService:AwardBadge(player.UserId, 2147668754)
				end
			end 
		end
	end
	
	return leaderboardData
end

game:GetService("ReplicatedStorage").GetLeaderboard.OnServerInvoke = function()
	return GetHighestRank()
end

Client-side:

local remoteFunction = game:GetService("ReplicatedStorage").GetLeaderboard

local function UpdateLeaderboard(leaderboardData)
	for _, entry in ipairs(leaderboardData) do
		local rank = entry.rank
		local name = entry.name
		local points = entry.points
		
		local tempCopy = script.Temp:Clone()
		tempCopy.Parent = script.Parent
		tempCopy.Name = rank
		tempCopy.Visible = true
		
		tempCopy.Position.Text = "["..rank.."]"
		tempCopy.Username.Text = game.Players:GetNameFromUserIdAsync(name)
		tempCopy.Value.Text = points
	end
end

remoteFunction.OnClientInvoke = UpdateLeaderboard
remoteFunction:InvokeServer()
1 Like

I got a blank leaderboard GUI once again. I got no errors in the console too.