Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- this script is for a l´global leaderboard (for the game 'Rainbow Ladder')
- ]]
- -- get the DataStoreService and create an ordered datastore named "WinsLeaderboard"
- local DataStoreService = game:GetService("DataStoreService")
- local WinsLeaderboard = DataStoreService:GetOrderedDataStore("WinsLeaderboard")
- -- updates the in-game leaderboard gui with top players from the datastore
- local function updateLeaderboard()
- local success, err = pcall(function()
- -- fetch the top 10 players (sorted in descending order by value)
- local ordered = WinsLeaderboard:GetSortedAsync(false, 11, 1)
- local page = ordered:GetCurrentPage()
- -- get GUI elements
- local FrameGui = workspace.Leaderboard.SurfaceGui.Frame
- local Podiums = FrameGui.Parent.Parent.Podiums
- -- clear previous leaderboard entries (except the "Info" frame)
- for _, v in FrameGui:GetChildren() do
- if v.ClassName == "Frame" and v.Name ~= "Info" then
- v:Destroy()
- end
- end
- -- loop through ranked data from the datastore
- for rank, data in page do
- local player = game.Players:GetPlayerByUserId(tonumber(data.key))
- local wins = data.value
- local isOnLeaderboard = false
- -- check if player is already on the leaderboard to avoid duplicates
- for _, v in FrameGui:GetChildren() do
- if v.ClassName == "Frame" and v.Name ~= "Info" then
- if v.Username.Text == player.Name then
- isOnLeaderboard = true
- break
- end
- end
- end
- -- if player has a valid score and is not yet shown on leaderboard
- if wins and not isOnLeaderboard then
- local podium
- -- choose podium style based on rank
- if rank == 1 then
- podium = Podiums.First:Clone()
- elseif rank == 2 then
- podium = Podiums.Second:Clone()
- elseif rank == 3 then
- podium = Podiums.Third:Clone()
- else
- podium = Podiums.Normal:Clone()
- end
- -- fill in leaderboard entry details
- podium.Rank.Text = tostring(rank)
- podium.Username.Text = player.Name
- podium.Wins.Text = tostring(wins)
- podium.LayoutOrder = rank
- podium.Parent = FrameGui
- end
- end
- end)
- -- warn if the leaderboard failed to update
- if err then
- warn("Failed to update leaderboard due to the following error: " .. err)
- end
- end
- -- saves a player’s win count to the ordered datastore
- local function saveWinsData(player: Player)
- pcall(function()
- WinsLeaderboard:SetAsync(player.UserId, player.leaderstats.Wins.Value)
- end)
- end
- -- background task: auto-save players' wins every 1 second
- task.spawn(function()
- while task.wait(1) do
- for _, player in game.Players:GetPlayers() do
- saveWinsData(player)
- end
- end
- end)
- -- background task: update the leaderboard display every 20 seconds
- task.spawn(function()
- while task.wait(20) do
- updateLeaderboard()
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement