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:
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()