How to script a Global Leaderboard

Here’s the whole model with scripts and all. Keep in mind only one of the sides as a script enabled (disabled the other three)
leaderboard.rbxm (15.3 KB)

:sweat_smile: I didn’t expected that but It will be easier

When you want to get the error message but also want the Script to resume

local success, errorMessage = pcall(ChangeDataStoreUsed)
if not success then
	warn(errorMessage)
end

That’s exactly what’s at line 278 to 281? But to my surprise it does neither of them in my actual game…

1 Like

I think I will need the entire game or Scripts that would communicate with (if there is) :frowning:

That’s sadly not possible, but I’ve tested by disabling all other scripts with no effect.

1 Like

So it doesn’t require others Scripts?

Not at all. It requires a database but it’s already created and saved. I also use the same scripts in my test place and my actual game.

1 Like

In the line 125 of the “Leaderboard Billboard Script” there is :

Leaderboard[item] = {value["key"], value["value"]}

I wonder if it should be that instead :

Leaderboard[item] = {key["key"], value["value"]}

That just causes an error.

It doesn’t make errors for me but I can’t really test it since I use the Baseplate world :smile:

That was with the exact model in a regular baseplate though. I beleive it would be easier to just rescript the whole leaderboard, would you help me with that?

Yes and I thought same before you said that! :joy: LOL

Alright, you got Discord? I believe it’s easier to communicate there.

Yes :slightly_smiling_face::slightly_smiling_face:

Add me, Tor#1329. Reply with your username so I know who to add (got 49 pending)

I’m nammed Léolol DB… so it will be a little bit obvious :slight_smile:

Is the Global leaderboard a free model? Some leaderboards only work with some type of a object, if it is the team create helper that made it, tell him or you to recheck the scripts.

As mentioned, you can change the w value. This is were the data is sourced from. It does not require playerPoints.

local w = plr.leaderstats.Cash.Value

Pulls player’s leaderstat cash value. plr is the player object.

More explanation: How to change where value is sourced from.

1 Like

For a leaderboard like that you are going to need 3 different items:

  1. Rank
  2. Username
  3. Points

Now we are going to break this down 1 by 1 on how we get each item

But first what you need to do is setup a basic DataStore to save the player’s points
Personally how I would do it is like this:

local DataStoreService = game:GetService("DataStoreService")
local PointsStore = DataStoreService:GetDataStore("PointsStore")

Now that we have a basic DataStore setup we can begin to use it, now for saving a player’s data, I like to load their data whenever they join the game and then save it whenever they leave, which is nice for saving bandwith. Here is an example of the code I would use.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local Points = PointsStore:GetAsync(Player.UserId)
	--Then you could set the in game value to the saved value, such as a number value
end)

Players.PlayerRemoved:Connect(function(Player)
	local Points = --This will equal the player's current points
	local Success, Err = pcall(function()
		PointsStore:SetAsync(Player.UserId, Points)
	end)
end)

Now that we have a basic storage system setup to save the player’s points we can now look into converting it into a leaderboard.
For the leaderboard we are going to be using Roblox’s built in leaderboard tool called OrderedDataStores

You can have another script setup here that handles the leaderboard

local Players = game:GetService("Players")

local PageSize = 10 --This is how many player's data will be on every page
local UpdateDelay = 60 --This is how often the leaderboard will update, minimum is 60 seconds
while true do
	local SortedStore = PointsStore:GetSortedAsync(false, PageSize)
	local Page = SortedStore:GetCurrentPage()
	for i,v in pairs(Page) do
		local Username = Players:GetUserIdFromNameAsync(v.key)
		local UserId = v.key
		local Place = I
		local Value = v.value
		--With this information you can now construct your Gui
	end
	wait(UpdateDelay)
end
3 Likes