DISCLAIMER: This is not for leaderstats, this is for a leaderboard for players with the most points for a leaderstat type.
##Global Leaderboards
What is a Global Leaderboard?
When I say Global Leaderboard, I don't mean leaderstats. Instead, I'm talking about those fancy boards in simulators or tycoons that show which user has the most money, xp, or event kills. This tutorial will include creation of a basic SurfaceGui and some coding.
##Creating the Model
First, we need to create a new part in workspace. Let's call this part "Leaderboard" so we can easily find it when we start coding.
Now we can start added our UI elements to the board. We need to add a SurfaceGui to our board. Make sure the face
element in the SurfaceGui has the correct face selected.
Create the following elements in the image below. Name them and put them in there correct parents.
Here are all the sizes and positions for the UI Elements
Frame
Size = {1, 0},{1, 0}
Position = {0, 0},{0, 0}
ScrollingFrame
Size = {1, 0},{1, 0}
Position = {0, 0},{0.2, 0}
Preset
Size = {1, 0},{0.075, 0}
Position = [[Overwritted by UIListLayout]]
Visible = false
Place
Size = {0.2, 0},{1, 0}
Position = {0.8, 0},{0, 0}
Points
Size = {0.8, 0},{0.5, 0}
Position = {0, 0},{0.5, 0}
UserName
Size = {0.8, 0},{0.5, 0}
Position = {0, 0},{0, 0}
TextLabel
Size = {1, 0},{0.2, 0}
Position = {0, 0},{0, 0}
Text = [[Make the text the title of your board. Example: Top Points Board]]
NOTE: You may customize the UI to however you want. Just remember that the script may not work with it if you change the names of things.
Your boad should look like the one shown below.
##Coding
Now we can begin with the coding section of the Tutorial. First, create a new script in ServerScriptService
After we create the script we can start creating our variables for the Leaderboard. Start with listed the DataStoreService and the Players. We need to define where the Leaderboard is. We will use :WaitForChild()
a bunch here so the game has time to load the Leaderboard. We also need to define a variable for the players that are currently in the server the board is in.
local OrderedDataStore = game:GetService("DataStoreService"):GetOrderedDataStore("WinsOrderedDataStore")
local Players = game:GetService("Players")
local Leaderboard = workspace:WaitForChild("Leaderboard")
local ScrollingFrame = Leaderboard:WaitForChild("SurfaceGui"):WaitForChild("Frame"):WaitForChild("ScrollingFrame")
local PresetPlayer = ScrollingFrame["Preset"]
local PlayersInGame = {}
Next, we can start writting our Function for Updating the Leaderboard. Create a new function with the name UpdateLeaderboard
. After this we need to define a variable called Pages. Now we create a pcall so our script wont error if the :GetSortedAsync()
fails. After tat we check if the Pages Variable is not equal to nil, basically checking if the pcall worked successfully. Then, define a new variable for the current page. Now we create a for loop to erase all the old data on the Leaderboard except for our Preset. After that we create another for loop to go through the new data recieved. In this for loop we define a variable for the Username. We create another pcall to set the username. After we set the username we check if the username pcall worked successfully. Then we create a tempframe by cloning our PresetPlayer variable. Then we set all the TextLabel's text to the correct data.
function UpdateLeaderboard()
local Pages
pcall(function()
Pages = OrderedDataStore:GetSortedAsync(false, 20)
end)
if Pages ~= nil then
local CurrentPage = Pages:GetCurrentPage()
for i,v in pairs(ScrollingFrame:GetChildren()) do
if v:IsA("Frame") and v.Name ~= "Preset" then
v:Destroy()
end
end
for i,data in ipairs(CurrentPage) do
local Username
pcall(function()
Username = Players:GetNameFromUserIdAsync(data["key"])
end)
if Username ~= nil then
local TempFrame = ToClone:Clone()
TempFrame.Parent = ScrollingFrame
TempFrame.Name = Username
TempFrame:WaitForChild("Place").Text = "[".. i.. "]"
TempFrame:WaitForChild("UserName").Text = Username
TempFrame:WaitForChild("Points").Text = data["value"]
TempFrame.Visible = true
end
end
end
end
Now we can create a new Function called NewPlayer
to set up the leaderstats for the board. This will also set the load the users saved data from when they left the game. We create variables with instances of a folder and an intvalue. We set the parent of the folder to the player, and the parent of the value to the folder. Remember to name the folder "leaderstats" so it will work correctly. Now we create a new variable for our Data. We run another pcall to set the Data. We check if the Data actually loaded. then we can set the Data Value's value to the Data
NOTE: If you are testing your Leaderboard in Roblox studio, make sure you enable API Services in the Security tab. Also make sure you are setting the value on the Server, and not the Client.
function NewPlayer(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Wins = Instance.new("IntValue", leaderstats)
Wins.Name = "Wins"
local WinsData
pcall(function()
WinsData = OrderedDataStore:GetAsync(player.UserId)
end)
if WinsData ~= nil then
Wins.Value = WinsData
end
UpdateLeaderboard()
end
Now we can create a new Function called PlayerLeaving
to save the player's data when the leave the game. We set a variable for to find the Data in the player's leaderstat folder that we created. If the Data is Greater than 1 then we will actually save it. This is because the defualt data is only 0. and we don't want a bunch of 0's on our Leaderboard. Then we can rerun our Leaderboard function to update the leaderboard.
function PlayerLeaving(player)
PlayersInGame[player] = nil
pcall(function()
local TempWins = player:WaitForChild("leaderstats"):WaitForChild("Wins").Value
if TempWins >= 1 then
OrderedDataStore:SetAsync(player.UserId, TempWins)
UpdateLeaderboard()
end
end)
end
After that we can connect our function to the events. In the playeradded event we can check the PlayersInGame list for our player to check if they were already in the game. If they weren't in the game we can set their data. After the PlayerAdded function we can create a new forloop to setup all the players that joined while the script was loading. Then after that we can finish it with conencting the PlayerRemoving event with our Function. Then, we can update our leaderboard.
Players.PlayerAdded:Connect(function(player)
if PlayersInGame[player] == nil then
NewPlayer(player)
end
end)
PlayersInGame = Players:GetPlayers()
for i,v in pairs(PlayersInGame) do
NewPlayer()
end
Players.PlayerRemoving:Connect(PlayerLeaving)
UpdateLeaderboard()
Hey! This was my first time creating a Tutorial on here. I hope everyone that reads it can understand it correctly. If you've found any errors with the tutorial please let me know. I would greaty appreciate if you could give some feedback on how I could improve my Tutorials in the future.
##NOTES & DISCLAIMERS
DISCLAIMER: This is not for leaderstats, this is for a leaderboard for players with the most points for a leaderstat type.
NOTE: You may customize the UI to however you want. Just remember that the script may not work with it if you change the names of things.
NOTE: If you are testing your Leaderboard in Roblox studio, make sure you enable API Services in the Security tab. Also make sure you are setting the value on the Server, and not the Client.