Advertisement
Tweak16

ROBLOX Scripting | Global Leaderboard

Jun 29th, 2020 (edited)
3,407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.54 KB | None | 0 0
  1. -- Made by Tweakified, Neonblox Games
  2. -- Video: https://www.youtube.com/watch?v=Ov-M41E5Nd0
  3.  
  4. local OrderedDataStore = game:GetService("DataStoreService"):GetOrderedDataStore("WinsOrderedDataStore")
  5. local Players = game:GetService("Players")
  6.  
  7. local WinsLeaderboard = workspace:WaitForChild("Wins Leaderboard")
  8. local ScrollingFrame = WinsLeaderboard:WaitForChild("Main"):WaitForChild("SurfaceGui"):WaitForChild("Frame"):WaitForChild("ScrollingFrame")
  9. local UIGridLayout = ScrollingFrame:WaitForChild("UIGridLayout")
  10.  
  11. local ToClone = script:WaitForChild("Temp")
  12.  
  13. local inGameStartupPlayers = {}
  14.  
  15. local function UpdateGuiLeaderboard()
  16.     local Pages
  17.     pcall(function()
  18.         Pages = OrderedDataStore:GetSortedAsync(false, 10)
  19.     end)
  20.     if Pages ~= nil then
  21.         local CurrentPage = Pages:GetCurrentPage()
  22.        
  23.         for i,v in pairs(ScrollingFrame:GetChildren()) do
  24.             if v:IsA("Frame") then
  25.                 v:Destroy()
  26.             end
  27.         end
  28.        
  29.         for i,data in ipairs(CurrentPage) do
  30.             local Username
  31.             pcall(function()
  32.                 Username = Players:GetNameFromUserIdAsync(data["key"])
  33.             end)
  34.             if Username ~= nil then
  35.                 local TempFrame = ToClone:Clone()
  36.                 TempFrame.Parent = ScrollingFrame
  37.                
  38.                 TempFrame:WaitForChild("Position").Text = "[".. i.. "]"
  39.                 TempFrame:WaitForChild("Username").Text = Username
  40.                 TempFrame:WaitForChild("Value").Text = data["value"]
  41.             end
  42.         end
  43.         local ScrollSize = Vector2.new(1, 0.1) * ScrollingFrame.AbsoluteSize
  44.         UIGridLayout.CellSize = UDim2.new(0, ScrollSize.X, 0, ScrollSize.Y)
  45.         ScrollingFrame.CanvasSize = UDim2.new(0, 0, 0, UIGridLayout.AbsoluteContentSize.Y)
  46.     end
  47. end
  48.  
  49. local function NewPlayer(player)
  50.     local leaderstats = Instance.new("Folder", player)
  51.     leaderstats.Name = "leaderstats"
  52.    
  53.     local Wins = Instance.new("IntValue", leaderstats)
  54.     Wins.Name = "Wins"
  55.    
  56.     local WinsData
  57.     pcall(function()
  58.         WinsData = OrderedDataStore:GetAsync(player.UserId)
  59.     end)
  60.     if WinsData ~= nil then
  61.         Wins.Value = WinsData
  62.     end
  63. end
  64.  
  65. local function PlayerLeaving(player)
  66.     inGameStartupPlayers[player] = nil
  67.    
  68.     pcall(function()
  69.         local TempWins = player:WaitForChild("leaderstats"):WaitForChild("Wins").Value
  70.         if TempWins >= 1 then
  71.             OrderedDataStore:SetAsync(player.UserId, TempWins)
  72.             UpdateGuiLeaderboard()
  73.         end
  74.     end)
  75. end
  76.  
  77. Players.PlayerAdded:Connect(function(player)
  78.     if inGameStartupPlayers[player] == nil then
  79.         NewPlayer(player)
  80.     end
  81. end)
  82.  
  83. inGameStartupPlayers = Players:GetPlayers()
  84. for i,v in pairs(inGameStartupPlayers) do
  85.     NewPlayer()
  86. end
  87.  
  88. Players.PlayerRemoving:Connect(PlayerLeaving)
  89. UpdateGuiLeaderboard()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement