Global leaderboard roblox studio
local DataStoreService = game:GetService("DataStoreService")
local GemsLeaderboard = DataStoreService:GetOrderedDataStore("GemLeaderboard")
local LastPosition = UDim2.new(0.5,0,0.07,0)
local Leaderboard = workspace.Lands.Forest.GlobalLeaderboard.LeaderboardGui.Holder
local function updateLeaderboard()
local success, errorMessage = pcall (function()
local Data = GemsLeaderboard:GetSortedAsync(false, 5)
local GemsPage = Data:GetCurrentPage()
for Rank, data in ipairs(GemsPage) do
local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
local Name = userName
local Gems = data.value
local isOnLeaderboard = false
for i, v in pairs(Leaderboard:GetChildren()) do
if v.Player.Text == Name then
isOnLeaderboard = true
break
end
end
if Gems and not isOnLeaderboard then
local newLbFrame = game.ServerStorage.LeaderboardTemplate:Clone()
newLbFrame.Player.Text = Name
newLbFrame.Gems.Text = Gems
newLbFrame.Rank.Text = Rank
newLbFrame.Parent = Leaderboard
if #Leaderboard:GetChildren() == 0 then
newLbFrame.Position = LastPosition
LastPosition = UDim2.new(0.5,0,0.07,0)
else
newLbFrame.Position = UDim2.new(0.5,0,LastPosition.Y.Scale + 0.13,0)
LastPosition = UDim2.new(0.5,0,LastPosition.Y.Scale + 0.13,0)
end
end
end
end)
if not success then
print(errorMessage)
end
end
while true do
for _, player in pairs(game.Players:GetPlayers()) do
GemsLeaderboard:SetAsync(player.UserId, player.leaderstats.Gems.Value)
end
for _, frame in pairs (Leaderboard:GetChildren()) do
frame:Destroy()
end
updateLeaderboard()
print("Leaderboard Updated!")
wait(160)
end