How to make a simple global leaderboard

You can absolutely store double values and numbers with decimals in datastores, the reason that won’t work is the leaderboard uses an OrderedDatastore to sort values easily. OrderedDatastores only are able to store integers, so they can be sorted easily. By using math.floor, you convert the double into a value the OrderedDatastore can use.

If you want to change the colors

Just modify the color values that are set in the following segment:

local color = Color3.new(1,1,1)--Default color
   	if number == 1 then
   		color = Color3.new(1,1,0)--1st place color
   	elseif number == 2 then
   		color = Color3.new(0.9,0.9,0.9)--2nd place color
   	elseif number == 3 then
   		color = Color3.fromRGB(166, 112, 0)--3rd place color
   	end

If you have any more questions, feel free to ask!

2 Likes

Sorry if I’m a bother but your tutorial is very great and it works perfectly!
But is there a way to add statues of the top 3 on the leaderboard? I wouldn’t know how to combine that plan with the script you made.

Thanks in advance and no problem if you can’t help me! The script itself is still really great and useful!

The script itself creates a frame for every player. If you have code that creates a statue, you can just check if it’s the first three players and then make a corresponding statue using their user id.

1 Like

i am still confused as to how to make the daily leaderboard

1 Like

Please read the FAQ before asking a question, as this has already been answered several times.

With a unique key, you can use that in this line:

local dataStore = dataStoreService:GetOrderedDataStore("Leaderboard" .. key)

Where you append the key to your leaderboard’s name.

1 Like

yeah but im confused because i want to make the leaderboard work with scores in the roblox leaderboard, and im asking where would i put it in the script that gets a new key everyday but also make it so that it only gets the amount of score a player got in the single day (since points in my game save), i just thought i would have to create a separate value and reset it daily, aslo, ince the player has their data saved, they would basically be on the leaderboard everyday, since it does not track the survivals they got in that game

Essentially, yes, you would store a second value which resets daily. Every time they gain a point, add to it. When they log on a new day, reset that value. Then, you can save the value with the unique key.

The unique key for the global datastore just makes sure that players who don’t log on on a given day don’t show up.

2 Likes

Now your answer does say we have to create a unique key every day, and you already have the lines for the creation of a new key, but where in the script would i put it? Under the variable that defines the data store? Or somewhere else such as in the while true do loop?

1 Like

Additionally, how could I make a value on the player reset everyday? I was thinking that you just reset it through all servers when a new day starts, but that would not work for if a player isn’t in the game, so make it that it resets the score at a time, even if the player isnt in game

1 Like

And one more thing, can i modify the leaderboard guis like the textlabels without breaking the script? Because I want to make it different

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

Where you define the retrieved datastore you would use the unique key. Note that if a server lasts for longer than 24 hours, you would want to update this again to avoid displaying data for the previous day.


Simply store the day that you’re collecting data for

--On Player Join
local dateTable = os.date("*t", os.time())
local keyForToday = "Y:"..dateTable["year"].." M:"..dateTable["month"].." D:"..dateTable["day"]
if playerData.lastTimeDataStored ~= keyForToday then
    playerData.dataValue = 0 --Reset the value
    playerData.lastTimeDataStored = keyForToday --Reset the value keeping track of the last reset
end
____
--When they get points
playerdata.dataValue += pointsAwarded

This just makes sure that if a player gained data on a previous day, that it resets before you save that value to the current day’s leaderboard. (If Player1 had 50 points the previous day, we should start at 0 today not 50)

You can customize the leaderboard however you like! Just be aware that if you change the names of stuff, you need to change them in the script aswell.

2 Likes

Thank you for helping but about what you said for the server lasting more than 24 hours and having to update the leaderboard, how would I do that, because most of the time on my game actually the server actually lasts 24 hours or when a new day starts (12am). Sorry for being a bit annoying to btw, i just really want a daily leaderboard in my game

2 Likes

You would just make a loop or however you want to go about it that every time the key changes, you set the dataStore variable to the new one.

1 Like

So do you have a line of code to explain that, because the only thoughts i have is to reset the data store daily like the value, or reset it when the data store changes (which i don’t know if that’s possible)

1 Like

I’m thinking it of something like changed

1 Like

Also does the leaderboard update the amount players get in day and not update only when a new day starts for example the daily leaderboard updates for new players to try and get on itso players get points while in game, would the leaderboard update the score they got every minute or something?

1 Like

You would just create a loop which runs every 60 seconds or however often (you could do it inside the actual loop in the code), which checks if the key has changed. If it has, update the dataStore variable.


Changed is for value objects, not variables. You would just check if previousValue ~= newKey.


This basic leaderboard just sorts the values YOU give it. Any more complicated behavior you have to add on your own, such as resetting the values you give it daily.

Resetting the key for getting the data JUST prevents you from displaying the previous day’s data. You also need to reset the value you give the leaderboard daily, with methods I’ve talked about above.


Suggestion

If you have any more questions such as exactly how to set up what I’ve said, your best bet would be asking in #help-and-feedback:scripting-support. I can explain the methods you would use, but I can’t make your game for you. Also, make sure to condense your replies into one instead of three in order to not create unnecessary post bloat. (You can edit replies if you need too)

2 Likes

How could I make this a leaderboard but only for people in the server?

2 Likes

Instead of using datastores, you would just save all the data into a table, and use table.sort to sort the data. Then, display it in the same way.

3 Likes

Thats my problem though… I saved it all in a dictionary and I am still stuck trying to figure out how I can sort it…

Here is what I mean:

local kills = {
["SpacePuppy2"] = 123 -- Say I have 123 kills.
["Player2"] = 999 -- They have 999 kills
["Player3"] = 0 -- They have zero kills.
}

I then get these values later by doing something like:

local killsValue = kills[playerName]

I tried using a function to turn them into arrays so I could sort them and it worked, but now I have no clue how to get the players kills from the array. If I could just figure this out my leaderboard should be working!

1 Like