Advertisement
BlonixOne

Global Leaderboard

May 18th, 2025
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | Source Code | 0 0
  1. --[[
  2. this script is for a l´global leaderboard (for the game 'Rainbow Ladder')
  3. ]]
  4.  
  5. -- get the DataStoreService and create an ordered datastore named "WinsLeaderboard"
  6. local DataStoreService = game:GetService("DataStoreService")
  7. local WinsLeaderboard = DataStoreService:GetOrderedDataStore("WinsLeaderboard")
  8.  
  9. -- updates the in-game leaderboard gui with top players from the datastore
  10. local function updateLeaderboard()
  11.     local success, err = pcall(function()
  12.         -- fetch the top 10 players (sorted in descending order by value)
  13.         local ordered = WinsLeaderboard:GetSortedAsync(false, 11, 1)
  14.         local page = ordered:GetCurrentPage()
  15.  
  16.         -- get GUI elements
  17.         local FrameGui = workspace.Leaderboard.SurfaceGui.Frame
  18.         local Podiums = FrameGui.Parent.Parent.Podiums
  19.  
  20.         -- clear previous leaderboard entries (except the "Info" frame)
  21.         for _, v in FrameGui:GetChildren() do
  22.             if v.ClassName == "Frame" and v.Name ~= "Info" then
  23.                 v:Destroy()
  24.             end
  25.         end
  26.  
  27.         -- loop through ranked data from the datastore
  28.         for rank, data in page do
  29.             local player = game.Players:GetPlayerByUserId(tonumber(data.key))
  30.             local wins = data.value
  31.             local isOnLeaderboard = false
  32.  
  33.             -- check if player is already on the leaderboard to avoid duplicates
  34.             for _, v in FrameGui:GetChildren() do
  35.                 if v.ClassName == "Frame" and v.Name ~= "Info" then
  36.                     if v.Username.Text == player.Name then
  37.                         isOnLeaderboard = true
  38.                         break
  39.                     end
  40.                 end
  41.             end
  42.  
  43.             -- if player has a valid score and is not yet shown on leaderboard
  44.             if wins and not isOnLeaderboard then
  45.                 local podium
  46.  
  47.                 -- choose podium style based on rank
  48.                 if rank == 1 then
  49.                     podium = Podiums.First:Clone()
  50.                 elseif rank == 2 then
  51.                     podium = Podiums.Second:Clone()
  52.                 elseif rank == 3 then
  53.                     podium = Podiums.Third:Clone()
  54.                 else
  55.                     podium = Podiums.Normal:Clone()
  56.                 end
  57.  
  58.                 -- fill in leaderboard entry details
  59.                 podium.Rank.Text = tostring(rank)
  60.                 podium.Username.Text = player.Name
  61.                 podium.Wins.Text = tostring(wins)
  62.                 podium.LayoutOrder = rank
  63.                 podium.Parent = FrameGui
  64.             end
  65.         end
  66.     end)
  67.  
  68.     -- warn if the leaderboard failed to update
  69.     if err then
  70.         warn("Failed to update leaderboard due to the following error: " .. err)
  71.     end
  72. end
  73.  
  74. -- saves a player’s win count to the ordered datastore
  75. local function saveWinsData(player: Player)
  76.     pcall(function()
  77.         WinsLeaderboard:SetAsync(player.UserId, player.leaderstats.Wins.Value)
  78.     end)
  79. end
  80.  
  81. -- background task: auto-save players' wins every 1 second
  82. task.spawn(function()
  83.     while task.wait(1) do
  84.         for _, player in game.Players:GetPlayers() do
  85.             saveWinsData(player)
  86.         end
  87.     end
  88. end)
  89.  
  90. -- background task: update the leaderboard display every 20 seconds
  91. task.spawn(function()
  92.     while task.wait(20) do
  93.         updateLeaderboard()
  94.     end
  95. end)
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement