FANDOM


The information on this page is outdated and needs to be updated.
Not to be confused with Player list.
Leaderboard

The ROBLOX leaderboard before August 27th, 2014

In ROBLOX, a leaderboard is a display of all the players and their stats and teams. Leaderboard stats often include KOs/WOs, Money, Points, Level, EXP, Time,

Example of Roblox leaderboard.

The ROBLOX leaderboard from 2006-2007.

Round Survived, etc. A leaderboard is created by placing a value named "leaderstats" inside of a player, and then by placing values inside of that. The leaderboard asset page is located here, with this being the item page.

Creating a Leaderboard

Creating a leaderboard is a fairly simple process. Create a script in the Workspace and put in it the following code.

function playerAdded(plr)

   local stats = Instance.new("IntValue", plr)
   stats.Name = "leaderstats"

   local money = Instance.new("IntValue", stats)
   money.Name = "Money"
   money.Value = 100

end

game.Players.PlayerAdded:connect(playerAdded)

That is all the code required for the most basic of leaderboards.

Changing Leaderboard Stats

Changing leaderboard stats is easy. All you have to do is get the player, and then change a value.

game.Players.Player1.leaderstats.Money.Value = game.Players.Player1.leaderstats.Money.Value + 10

Saving and Loading Stats

Data Persistence Method

Saving and loading stats is slightly more complicated, but overall not very difficult to add to your leaderboard. You first need to add two functions, saveInt and loadInt. Note: There are several ways to do this, I'm just showing one.

function saveInt(player, score, key)
   player:SaveNumber(key, score)
end

function loadInt(player, stat, key) 
   stat.Value = player:LoadNumber(key)
end

After that, you need to change your playerAdded function.

function playerAdded(plr)

   local stats = Instance.new("IntValue", plr)
   stats.Name = "leaderstats"

   local money = Instance.new("IntValue", stats)
   money.Name = "Money"
   money.Value = 100

   plr:WaitForDataReady()

   loadInt(plr, money, "Money")

end

The two new lines will first wait for the player's data to be ready, and then load the saved "Money" stat. After that you need to use the .PlayerRemoving event to save a player's data before they leave.

function playerRemoving(plr)

   local stats = plr:FindFirstChild("leaderstats")
   if stats ~= nil then
      saveInt(player, stats.Money.Value, "Money")
   end

end

game.Players.PlayerRemoving:connect(playerRemoving)

And you're done. You now have a working leaderboard that saves and loads a player's data, and the knowledge of how to edit it to your needs.

Datastore Method

Making a Datastore leaderboard is fairly similar to the previous method, so if you need an explanation of the functions, refer to the previous section.

local boardstore = game:GetService("DataStoreService"):GetDataStore("Board Store")

function saveInt(player, score, key)
	if player.userId > 0 then
		boardstore:SetAsync(player.userId .. " " .. key, score)
	end
end

function loadInt(player, stat, key)
	if player.userId > 0 then
		stat.Value = boardstore:GetAsync(player.userId .. " " .. key)
	end
end

function playerAdded(plr)
	local stats = Instance.new("IntValue", plr)
	stats.Name = "leaderstats"
	
	local money = Instance.new("IntValue", stats)
	money.Name = "Money"
	
	plr:WaitForDataReady()
	
	loadInt(plr, money, "Money")
end

function playerRemoving(plr)
	local stats = plr:FindFirstChild("leaderstats")
	if stats ~= nil then
		saveInt(plr, stats.Money.Value, "Money")
	end
end

game.Players.PlayerAdded:connect(playerAdded)
game.Players.PlayerRemoving:connect(playerRemoving)

The difference is that we are defining the store with local boardstore = game:GetService("DataStoreService"):GetDataStore("Board Store"), which is where we store our data. Datastore does support Guest save, but for this example, the user's ID must be greater than 0 to have a save. This is because

  • If the guest clears his/her cookies, he/she would need to restore the GuestData cookie in order to recover the data.
  • If someone else acquires their guest ID and edits their GuestData Cookie to match the ID, they can inherit the data they have stored.

Also, we save/load with a datastore when a player enters/leaves the server.

External Sources

Blog

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.