KavonPricingHelpUpdatesAbout usFAQ
Log inStart freeOpen chat
Guide

Roblox leaderboard script with saving (DataStore)

The built-in Roblox leaderboard shows any value inside a folder named leaderstats on the player. To keep the numbers between sessions, you save them in a DataStore. Here is a version that does both.

The script

Put this in a Script inside ServerScriptService. It makes a Coins stat, loads the saved value when a player joins, and saves it when they leave or the server shuts down.

-- ServerScriptService/Leaderboard
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local store = DataStoreService:GetDataStore("PlayerCoins")

local function save(player)
	local coins = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Coins")
	if not coins then return end
	local ok, err = pcall(function()
		store:SetAsync(tostring(player.UserId), coins.Value)
	end)
	if not ok then warn("Could not save coins for " .. player.Name .. ": " .. tostring(err)) end
end

Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = player

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = stats

	local ok, saved = pcall(function()
		return store:GetAsync(tostring(player.UserId))
	end)
	coins.Value = (ok and saved) or 0
end)

Players.PlayerRemoving:Connect(save)

game:BindToClose(function()
	for _, player in ipairs(Players:GetPlayers()) do
		save(player)
	end
end)

What each part does

  • leaderstats folder. Roblox shows its values in the player list automatically. The name has to be exactly leaderstats.
  • pcall. DataStore calls can fail, so they are wrapped. Without it, one failed request would stop the script.
  • PlayerRemoving and BindToClose. The first saves a player who leaves. The second saves everyone when the server closes, so nobody loses progress.

Two things that trip people up

  • Studio access. To test DataStores inside Studio, turn on "Enable Studio Access to API Services" in your game's Security settings.
  • Saving too often. DataStores have rate limits. Save on leaving and on shutdown, and optionally every few minutes, not on every coin.

Adding more stats

For Kills, Level or Gems, you need another IntValue for each and a table to save them together. That is where hand-written code gets longer and easier to break.

Or ask Kavon

Describe what you want, for example:

Add a leaderboard with Coins, Kills and Level. Save all three with a DataStore, and give 10 coins for every kill.

Kavon looks at your existing scripts first, so the new leaderboard works with the coin and kill systems you already have.

Try it yourself. Kavon is free to start: 10 free credits, no card needed, and every change can be undone with Ctrl+Z.

Start free

More guides

  • Roblox AI script generator
  • Roblox Studio AI plugin
  • Roblox shop system script
  • Fix Roblox script errors with AI
© Kavon AIHelp   Updates   Privacy   Terms   About us