How to make a simple global leaderboard

Glad you found it useful! If you have any questions, feel free to DM them to me.

19 Likes

Wow this is really nice. Good job! How long did it take you to write this?

9 Likes

I’ve already made countless leaderboards, the problem was trying to make it easy to understand. It took me about 2 hours to get the images and text done. Glad you found it useful.

18 Likes

This is an amazing tutorial on how to make a global leaderboard! I have a question. Why are you using PointsService to handle the ranking? Isn’t it deprecated? (Sorry, about this because you wrote in your post to PM you if there are any questions)

6 Likes

PointsService doesn’t handle the ranking. It’s just the score you can have. You can change it, for example:
local w = player.leaderstats.Points.Value

6 Likes

Yes, it is depreciated. I wanted to use something simple so I did not have to go through how data stores work ect.

Change this line of code:

	for i,plr in pairs(game.Players:GetChildren()) do--Loop through players
		if plr.UserId>0 then--Prevent errors
			local ps = game:GetService("PointsService")--PointsService
>>>>>>>>>>>	local w = ps:GetGamePointBalance(plr.UserId)--Get point balance
			if w then
				pcall(function()
				--Wrap in a pcall so if Roblox is down, it won't error and break.
					dataStore:UpdateAsync(plr.UserId,function(oldVal)
				        --Set new value
						return tonumber(w)
					end)
				end)
			end
		end
	end

If cash was a leaderstat for players, and you wanted to make a leaderboard for it:

local w = plr.leaderstats.Cash.Value

Note: Also remove were pointsService is defined if you do not need it.

If you have more questions, feel free to DM me.

18 Likes

Please help me, I want the list to come out in order but they only come out cluttered, I’ve put “true” where it says that the small numbers appear first but it does not work, they keep coming out just as messy

2 Likes

I have no idea how this went 2 months without anyone saying anything. Change these lines:

local new = sample:Clone()
new.Name = number
--Add this line:
new.LayoutOrder = tonumber(number)

Then, go to ScrollingFrame.UI and change ScrollingFrame.UI.SortOrder to ‘LayoutOrder’.

Roblox’s name ordering system puts 10 after 1. This is why I should of used LayoutOrder.

This should be fixed in the original model now, so you can sort backwards.

7 Likes

Awesome tutorial, I’ve managed to make my global (all-time) leaderboards work perfectly. However I’m also trying to make a daily & weekly leaderboard, but still confuzzled about it. For the all-time leaderboards, I used a value “TotalCash”. This is in the player’s data, and is added to every time the player earns some cash. Though obviously I wouldn’t be able to use it for daily leaderboards, hence why I’m unsure of what to do. I tried looking for some examples of them but couldn’t find any :confused: .Would highly appreciate some ideas on how I could make my daily and weekly leaderboards.

4 Likes

For this simply change the name of the OrderedDataStore based on the week. You can generate a unique weekly-id by dividing the current epoch-time with the number of seconds in a week, then rounding this value:

local dataStoreName = "LeaderboardWeek"
local restartWeekOffset = 86400 * 1 -- Friday (0), Saturday(1), Sunday(2), Monday(3), Tuesday(4), Wednesday(5), Thursday(6)
local secondsInAWeek = 604800
local currentTime = os.time()
local weekId = math.floor((currentTime+restartWeekOffset)/secondsInAWeek)
local finalName = dataStoreName..weekId
local orderedDataStore = dataStoreService:GetOrderedDataStore(finalName)

Same principle applies for a daily-leaderboard, where you’d divide through by the number of seconds in a day, instead of a week.

19 Likes

Just discovered this tutorial now, thanks for creating! Definitely will be trying this out later. :smile:

6 Likes

Ah I get it, it simply changes the OrderedDataStore each week. That’s cool, thanks!

5 Likes

A Mathy Method

@ForeverHD’s Method will work, but you can also utilize os.date to make your life a bit easier instead of doing a ton of math. os.date also factors in leap-days, daylight savings and such.

A Easier Method

Basically, what you do is instead of overwriting data, you just save and get it in a different key. That way, you don’t have to wory about past days or years or anything, you can just change the key. Using os.time and os.date, we make can make a unique key for the day, month, or year.

To acheive daily leaderboards, simply create a unique key for each day using os.date(), storing the year, month, and day. (year because if the game lasts for a year, old data will be used)

local dateTable = os.date("*t", os.time())
local key = "Y:"..dateTable["year"].." M:"..dateTable["month"].." D:"..dateTable["day"]
--Unique key for each day

In case you didn’t see it, My FAQ section has a bullet on this:

How do I make a daily, monthly, or yearly leaderboard?

To make leaderboards “Reset” we can simply change where we save and get the data from. os.time() gives us the number of seconds since the unix equinox. os.date(time) convertes those seconds into a date.

To acheive daily leaderboards, simply create a unique key 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 key for each day

To acheive monthly leaderboards, simply create a unique key for each month using os.date()

local dateTable = os.date("*t", os.time())
local key = "Y:"..dateTable["year"].." M:"..dateTable["month"]
--Unique key for each month

To acheive yearly leaderboards, simply create a unique key for each year using os.date()

local dateTable = os.date("*t", os.time())
local key = "Y:"..dateTable["year"]
--Unique key for each year
14 Likes

Your welcome! Hope you find it useful. If you have any questions, feel free to ask them.

6 Likes

If I understand this correctly, would the key be the name of the ordered datastore? As it’ll be changing daily/weekly.

Also, I’ve created a value for the player’s Daily and Weekly data. How would I reset these every day and week? image

3 Likes

You could store it like this, and reset when the two value’s don’t equal each other.

Here’s a little pseudo code

local dateTable = os.date("*t", os.time())
local key = "Y:"..dateTable["year"].." M:"..dateTable["month"].." D:"..dateTable["day"]
local getData = --Get player's data
if getData["DayStored"]~=key then
    --New day, reset data and set new key
    getData["DayStored"] = key
    getData["ActualData"] = 0 --Add to this when they gain cash
end
6 Likes

That’s easier than I thought haha, thank you very much!

5 Likes

Glad you found it useful. The same principle would apply for other values, just remember to reset when the day is not the same, and create a separate value to save to other than your long-term storage.

7 Likes

How do I change it to display Money?

5 Likes

Hello! I want to know how do i set a line where i can set what players cannot appear in the leaderboard, some kind of banned people or developers who wants to test stuff with a high value of “Rebirths” without appearing on the leaderboard.

3 Likes