Global leaderboards are great for adding competition come a game. Players want to become the ideal of castle all, and also will work hard to complete to end up being the best. If friend make your leaderboard watch nice, players will carry out anything to be far better than the rest.

You are watching: Roblox how to make a leaderboard


*

Taken from mine game, The Tower TycoonJust look exactly how hard human being worked just to have a number and their name displayed on a digital board.

Before you Begin

For this tutorial, the is encourage you recognize the basics of manipulating components in studio and also making a surface ar gui .

This tutorial is based turn off of pointsService for simplicity sake. This have the right to be changed. For much more info, check the FAQ in ~ the bottom once you have read through. For now, don’t worry around it.

I don"t desire to read the tutorial, just give me the leaderboard

Don’t desire to do the physical leaderboard and also just want to code it? Skip come the coding section (has download for an easy leaderboard without script). Don’t want to execute that either? Download because that the totality thing is in ~ the end. (FAQ is accessible there too)

Let’s Begin!

With that out of the way, welcome. Now I will be reflecting you how to do a nice global leaderboard come let your players contend to come to be the best.

Surface GUI Designing

The very first step come making a global leaderboard is to make the physics part it will certainly be on. It deserve to be any type of size, and you can add cylinders to develop a rounded leaderboard, adjust colors, add transparency, and also more. For this tutorial however, we will use a straightforward 6x9x1 part. The is crucial to do the leaderboard look nice and also appealing, however this deserve to be excellent later. Because that now, let’s save it simple.

Add a UIListLayout come the Scrolling Frame. Name it “UI”, and collection SortOrder to LayoutOrder.

*

There’s one an ext thing we should do before we gain into the code; We require a framework to display the information. Do a sample framework such together the below one (Called “Sample”), and have imageLabel called “Image”, and also 3 textLabels called “Place”, “PName”, and also “Value”. Parental “Place” come the Image, together it will certainly make it easier to range (Scale & Pos = 0.5, 0,0.5, 0).

We now have actually a Surface GUI it is ready to it is in scripted! include a script (not regional Script) to the surface ar gui. Parental the structure “Sample”.

*

It’s Coding Time!

Time to code! at this point, friend should have a leaderboard like this: Leaderboard without Script.rbxl (21.7 KB).

We need to define every one of our variables first, so us can conveniently reference items. (You have the right to make your variable names longer, yet I prefer much shorter names. If you deserve to remember them, use them.)

local sg = script.Parent --Surface GUIlocal sample = script:WaitForChild("Sample") --Our Sample framelocal sf = sg:WaitForChild("ScrollingFrame") --The scrolling framelocal ui = sf:WaitForChild("UI") --The UI perform layoutNest, we require to pick the surname for our worldwide Ordered Data keep and set it to a variable. This will enable us to usage aufdercouch.net to kind the data because that us, without having actually to worry around thousands that entries to type through. Note: an altering the key will change where aufdercouch.net fetches our data from. This is helpful for having multiple leaderboards, or simply resetting data.

local dataStoreService = game:GetService("DataStoreService")--The data store servicelocal dataStore = dataStoreService:GetOrderedDataStore("Leaderboard")--Get the data save with crucial "Leaderboard"--Want to make different leaderboards?--Change this an essential to a various string ("Leaderboard2", ect.), and you can have many leaderboards!I want day-to-day leaderboards!Not all human being want all-time leaderboards. To acheive daily leaderboards, simply produce a unique an essential for each day using os.date()

local dateTable = os.date("*t", os.time())local key = "Y:"..dateTable<"year">.." M:"..dateTable<"month">.." D:"..dateTable<"day">--Unique crucial for every dayNow, start a loop to upgrade data and fetch data.

wait(5)while true do--will add wait(120) at the end--loop the runs every 2 minutesThen, we need to update the data. Us loops with all the players, and also update their data. In this tutorial, we usage player points as the data to save (How To compensation Player Points). However, friend can easily reroute this to feature and also value by specifying w as something else.

for i,plr in pairs(game.Players:GetChildren()) do--Loop v playersif plr.UserId>0 then--Prevent errors neighborhood ps = game:GetService("PointsService")--PointsServicelocal w = ps:GetGamePointBalance(plr.UserId)--Get allude balanceif w thenpcall(function() --Wrap in a pcall for this reason if aufdercouch.net is down, that won"t error and break.dataStore:UpdateAsync(plr.UserId,function(oldVal) --Set brand-new valuereturn tonumber(w)end)end)endendendNow, we should fetch our data. We have the right to do this by utilizing our dataStore we identified earlier.

neighborhood smallestFirst = false--false = 2 prior to 1, true = 1 prior to 2 neighborhood numberToShow = 100--Any number in between 1-100, how numerous will be displayed local minValue = 1--Any numbers reduced than this will certainly be excluded regional maxValue = 10e30--(10^30), any kind of numbers higher than this will certainly be excluded neighborhood pages = dataStore:GetSortedAsync(smallestFirst, numberToShow, minValue, maxValue) --Get data neighborhood top = pages:GetCurrentPage()--Get the first pageYay! we now have actually our data, nice and also organized v it’s crucial (userid), and value (points). But wait, we don’t have the player’s photo or username! We must fetch those before displaying ours data.

See more: What Are All The Factors Of 55 ? Find Prime Factorization/Factors Of 55

local data = --Store brand-new data because that a,b in ipairs(top) do--Loop through data regional userid = b.key--User id local points = b.value--Points local username = ""--If that fails, us let them recognize local s,e = pcall(function() username = game.Players:GetNameFromUserIdAsync(userid)--Get username end) if not s then--Something walk wrong warn("Error acquiring name for "..userid..". Error: "..e) finish local image = game.Players:GetUserThumbnailAsync(userid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150) --Make a photo of them table.insert(data,username,points,image)--Put brand-new data in new table endOur data is every organized and also tidy! Now, we desire to take it this tidy data and also display it! (username, points, image)

ui.Parent = scriptsf:ClearAllChildren()--Remove old framesui.Parent = sffor number,d in pairs(data) do--Loop with our brand-new datalocal surname = d<1>local val = d<2>local image = d<3>local color = Color3.new(1,1,1)--Default colorif number == 1 thencolor = Color3.new(1,1,0)--1st location colorelseif number == 2 thencolor = Color3.new(0.9,0.9,0.9)--2nd ar colorelseif number == 3 thencolor = Color3.fromRGB(166, 112, 0)--3rd location colorendlocal brand-new = sample:Clone()--Make a clone that the sample framenew.Name = name--Set surname for better recognition and also debugging new.LayoutOrder = number--UIListLayout supplies this to kind in the correct ordernew.Image.Image = image--Set the imagenew.Image.Place.Text = number--Set the placenew.Image.Place.TextColor3 = color--Set the place color (Gold = 1st)new.PName.Text = name--Set the usernamenew.Value.Text = val--Set the quantity of pointsnew.Value.TextColor3 = color--Set the place color (Gold = 1st)new.PName.TextColor3 = color--Set the place color (Gold = 1st)new.Parent = sf--Parent come scrolling frameendwait()sf.CanvasSize = UDim2.new(0,0,0,ui.AbsoluteContentSize.Y)--Give sufficient room because that the frames come sit in wait(120)end--End the if loop

Our complete code masterpiece!

local sample = script:WaitForChild("Sample") --Our Sample framelocal sf = sg:WaitForChild("ScrollingFrame") --The scrolling framelocal ui = sf:WaitForChild("UI") --The UI perform layoutlocal dataStoreService = game:GetService("DataStoreService")--The data save servicelocal dataStore = dataStoreService:GetOrderedDataStore("Leaderboard")--Get the data save with an essential "Leaderboard"wait(10)while true dofor i,plr in pairs(game.Players:GetChildren()) do--Loop v playersif plr.UserId>0 then--Prevent errorslocal ps = game:GetService("PointsService")--PointsServicelocal w = ps:GetGamePointBalance(plr.UserId)--Get suggest balanceif w thenpcall(function()--Wrap in a pcall for this reason if aufdercouch.net is down, the won"t error and also break.dataStore:UpdateAsync(plr.UserId,function(oldVal) --Set new valuereturn tonumber(w)end)end)endendend regional smallestFirst = false--false = 2 prior to 1, true = 1 before 2 local numberToShow = 100--Any number between 1-100, how many will be shown local minValue = 1--Any numbers reduced than this will certainly be excluded neighborhood maxValue = 10e30--(10^30), any numbers greater than this will be excluded local pages = dataStore:GetSortedAsync(smallestFirst, numberToShow, minValue, maxValue) --Get data local top = pages:GetCurrentPage()--Get the first pagelocal data = --Store new datafor a,b in ipairs(top) do--Loop v datalocal userid = b.key--User idlocal clues = b.value--Pointslocal username = ""--If the fails, we let castle knowlocal s,e = pcall(function() username = game.Players:GetNameFromUserIdAsync(userid)--Get usernameend)if not s then--Something walk wrong warn("Error acquiring name because that "..userid..". Error: "..e)endlocal picture = game.Players:GetUserThumbnailAsync(userid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)--Make a image of themtable.insert(data,username,points,image)--Put brand-new data in brand-new tableendui.Parent = scriptsf:ClearAllChildren()--Remove old framesui.Parent = sffor number,d in pairs(data) do--Loop v our brand-new datalocal name = d<1>local val = d<2>local picture = d<3>local color = Color3.new(1,1,1)--Default colorif number == 1 thencolor = Color3.new(1,1,0)--1st location colorelseif number == 2 thencolor = Color3.new(0.9,0.9,0.9)--2nd ar colorelseif number == 3 thencolor = Color3.fromRGB(166, 112, 0)--3rd place colorendlocal new = sample:Clone()--Make a clone of the sample framenew.Name = name--Set surname for better recognition and also debugging new.LayoutOrder = number--UIListLayout offers this to sort in the exactly ordernew.Image.Image = image--Set the imagenew.Image.Place.Text = number--Set the placenew.Image.Place.TextColor3 = color--Set the place color (Gold = 1st)new.PName.Text = name--Set the usernamenew.Value.Text = val--Set the amount of pointsnew.Value.TextColor3 = color--Set the place color (Gold = 1st)new.PName.TextColor3 = color--Set the place shade (Gold = 1st)new.Parent = sf--Parent to scrolling frameendwait()sf.CanvasSize = UDim2.new(0,0,0,ui.AbsoluteContentSize.Y)--Give sufficient room because that the frames come sit in wait(120)endI desire the top bar from the very first photo!For those wanting a peak bar, you deserve to duplicate the sample frame and put it outside of the scrolling frame. Remember, you deserve to mess v colors and text, and also make a yes, really cool leaderboard.

FAQ (Frequently request Questions)

How perform I do multiple leaderboards?

Duplicate the leaderboard, and also then to express to over to set a different scope: