Global leaderboards are great for including competition to a game. Players want to end up being the finest of them all, and will work difficult to contend to come to be the best. If girlfriend make your leaderboard watch nice, players will perform anything to be better than the rest.

You are watching: Roblox how to make a leaderboard


*

Taken from mine game, The Tower TycoonJust look exactly how hard world worked simply to have a number and also their name presented on a online board.

Before you Begin

For this tutorial, it is encourage you know the basics that manipulating parts in studio and making a surface ar gui .

This tutorial is based off of pointsService because that simplicity sake. This have the right to be changed. For more info, inspect the FAQ at the bottom as soon as you have read through. Because that now, don’t worry around it.

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

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

Let’s Begin!

With that the end of the way, welcome. Now I will certainly be mirroring you how to do a nice an international leaderboard come let her players compete to end up being the best.

Surface GUI Designing

The first step come making a worldwide leaderboard is come make the physical part it will certainly be on. It deserve to be any type of size, and also you can include cylinders to create a rounded leaderboard, adjust colors, include transparency, and more. For this tutorial however, we will usage a an easy 6x9x1 part. The is necessary to do the leaderboard watch nice and also appealing, but this have the right to be excellent later. For now, let’s keep it simple.

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

*

There’s one much more thing we need to do prior to we obtain into the code; We need a frame to screen the information. Make a sample structure such as the listed below one (Called “Sample”), and have imageLabel called “Image”, and 3 textLabels called “Place”, “PName”, and “Value”. Parental “Place” come the Image, as it will make it easier to scale (Scale & Pos = 0.5, 0,0.5, 0).

We now have a Surface GUI that’s ready to it is in scripted! include a script (not regional Script) come the surface gui. Parental the frame “Sample”.

*

It’s Coding Time!

Time come code! in ~ this point, friend should have a leaderboard choose this: Leaderboard without Script.rbxl (21.7 KB).

We should define every one of our variables first, so us can conveniently reference items. (You can make her variable name longer, yet I prefer much shorter names. If you deserve to remember them, usage 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 need to select the name for our global Ordered Data save and set it come a variable. This will permit us to usage cg-tower.com to type the data because that us, without having actually to worry about thousands the entries to type through. Note: changing the key will readjust where cg-tower.com fetches our data from. This is helpful for having multiple leaderboards, or just resetting data.

local dataStoreService = game:GetService("DataStoreService")--The data keep servicelocal dataStore = dataStoreService:GetOrderedDataStore("Leaderboard")--Get the data keep with crucial "Leaderboard"--Want to make different leaderboards?--Change this an essential to a various string ("Leaderboard2", ect.), and you have the right to have many leaderboards!I want day-to-day leaderboards!Not all civilization want all-time leaderboards. To acheive daily leaderboards, simply create a unique crucial for every day making use of os.date()

local dateTable = os.date("*t", os.time())local crucial = "Y:"..dateTable<"year">.." M:"..dateTable<"month">.." D:"..dateTable<"day">--Unique an essential for each dayNow, begin 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 have to update the data. Us loops v all the players, and also update your data. In this tutorial, we usage player points together the data to save (How To award Player Points). However, friend can conveniently reroute this to feature and value by defining w as something else.

for i,plr in pairs(game.Players:GetChildren()) do--Loop through 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 therefore if cg-tower.com is down, the won"t error and also break.dataStore:UpdateAsync(plr.UserId,function(oldVal) --Set new valuereturn tonumber(w)end)end)endendendNow, we need to fetch our data. We deserve to do this by utilizing our dataStore we characterized earlier.

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

See more: How To Say Cast In Spanish Translation Of “Cast”, How To Say Cast In Spanish

regional data = --Store brand-new data because that a,b in ipairs(top) do--Loop v data neighborhood userid = b.key--User id local points = b.value--Points local username = ""--If the fails, us let them know local s,e = pcall(function() username = game.Players:GetNameFromUserIdAsync(userid)--Get username end) if not s then--Something walk wrong warn("Error gaining name because that "..userid..". Error: "..e) end local photo = game.Players:GetUserThumbnailAsync(userid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150) --Make a picture of castle table.insert(data,username,points,image)--Put new data in brand-new table endOur data is all organized and also tidy! Now, we want to take 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 through our new datalocal name = d<1>local val = d<2>local photo = d<3>local shade = Color3.new(1,1,1)--Default colorif number == 1 thencolor = Color3.new(1,1,0)--1st place colorelseif number == 2 thencolor = Color3.new(0.9,0.9,0.9)--2nd location colorelseif number == 3 thencolor = Color3.fromRGB(166, 112, 0)--3rd place 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 uses this to sort in the correct ordernew.Image.Image = image--Set the imagenew.Image.Place.Text = number--Set the placenew.Image.Place.TextColor3 = color--Set the place shade (Gold = 1st)new.PName.Text = name--Set the usernamenew.Value.Text = val--Set the lot of pointsnew.Value.TextColor3 = color--Set the place shade (Gold = 1st)new.PName.TextColor3 = color--Set the place shade (Gold = 1st)new.Parent = sf--Parent come scrolling frameendwait()sf.CanvasSize = UDim2.new(0,0,0,ui.AbsoluteContentSize.Y)--Give enough room for the frames come sit in wait(120)end--End the while loop

Our full 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 store servicelocal dataStore = dataStoreService:GetOrderedDataStore("Leaderboard")--Get the data save with key "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 allude balanceif w thenpcall(function()--Wrap in a pcall so if cg-tower.com is down, the won"t error and break.dataStore:UpdateAsync(plr.UserId,function(oldVal) --Set brand-new valuereturn tonumber(w)end)end)endendend local smallestFirst = false--false = 2 before 1, true = 1 before 2 local numberToShow = 100--Any number in between 1-100, how numerous will be displayed local minValue = 1--Any numbers lower than this will be excluded neighborhood maxValue = 10e30--(10^30), any numbers greater than this will certainly be excluded neighborhood 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 with datalocal userid = b.key--User idlocal points = b.value--Pointslocal username = ""--If it fails, we let castle knowlocal s,e = pcall(function() username = game.Players:GetNameFromUserIdAsync(userid)--Get usernameend)if not s then--Something go wrong warn("Error acquiring name because that "..userid..". Error: "..e)endlocal image = game.Players:GetUserThumbnailAsync(userid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)--Make a image of themtable.insert(data,username,points,image)--Put new data in brand-new tableendui.Parent = scriptsf:ClearAllChildren()--Remove old framesui.Parent = sffor number,d in pairs(data) do--Loop with 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 ar colorelseif number == 2 thencolor = Color3.new(0.9,0.9,0.9)--2nd place colorelseif number == 3 thencolor = Color3.fromRGB(166, 112, 0)--3rd ar colorendlocal new = sample:Clone()--Make a clone that the sample framenew.Name = name--Set surname for far better recognition and debugging new.LayoutOrder = number--UIListLayout supplies this to type 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 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 come scrolling frameendwait()sf.CanvasSize = UDim2.new(0,0,0,ui.AbsoluteContentSize.Y)--Give sufficient room for the frames to sit in wait(120)endI desire the peak bar from the first photo!For those wanting a top bar, you deserve to duplicate the sample frame and put it outside of the scrolling frame. Remember, you deserve to mess v colors and also text, and also make a really cool leaderboard.

FAQ (Frequently inquiry Questions)

How perform I do multiple leaderboards?

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