How to make a simple global leaderboard

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.

20 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.

6 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

I discuss in detail how to do this in the FAQ section. Please read this before asking a question.

3 Likes

The easiest way to do this is check if a players name is on the list, and if it is, send the value 0 to the function.

In the reply above, I linked to the FAQ section which explains how to modify the value in detail.

Another solution is to change the max value to exclude those values.

6 Likes

Is there a way to return multiple values from the table so I can get say players rank.Value and Kills.Value?

4 Likes

Yes, but you have to make multiple copies of the script, and change where the data is stored (:GetOrderedDataStore(name)).

OrderedDataStores can only store a single numerical value, so that’s the best possible solution using Roblox, without owning your own data manager.

7 Likes

Okay I might just update the server every time the players kills.value or rank.value stat changes instead

2 Likes

Word of warning: You should limit your updates to at least once every 60 seconds, as there are data store request limits.

To clarify: This is only for data store values, value object are not subject to the limit.

Limits in detail (You have to scroll down): Documentation - Roblox Creator Hub

7 Likes

The global leaderboard isn’t working for me. It’s giving me this error in the output:


I’m not sure what that means…

2 Likes