How do I add a datastore to my leaderboard?

Simple enough, I was wondering how I would be able to add a datastore to my leaderboard, so once they join the game it loads their previous data, and if they left it saves the data. That’s all!

(simply, if they left with 5 smacks they rejoin with 5 smacks instead of 0)

Heres my current script:

local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Punches = Instance.new("IntValue")
	Punches.Name = "Smacks"
	Punches.Value = 0
	Punches.Parent = leaderstats
	
	local Ball = Instance.new("StringValue")
	Ball.Name = "Ball"
	Ball.Value = "Default"
	Ball.Parent = leaderstats
end

Players.PlayerAdded:Connect(leaderboardSetup)
6 Likes

It gets quite complex, which is why I’d recommend modules like DataStore2, which can be found under community resources on the DevForum.

However, if you want to do it yourself, here you go:

This topic has many replies that describe how data saving works and is worth looking into for learning more about DataStores.

6 Likes

I didn’t really get an answer from one of the responses.

2 Likes
local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local LeaderboardDataStore = DataStore:GetDataStore("LeaderboardData") -- We get the datastore that being make one.

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Punches = Instance.new("IntValue")
	Punches.Name = "Smacks"
	Punches.Value = 0
	Punches.Parent = leaderstats

	local Ball = Instance.new("StringValue")
	Ball.Name = "Ball"
	Ball.Value = "Default"
	Ball.Parent = leaderstats

	local success,result = pcall(function() -- This function is use to prevent errors in console and to see if error did occur.
		return LeaderboardDataStore:GetAsync(player.UserId) -- We get our data.
	end)
	if success then -- We see if an error occurs or not.
		if result then -- We see if player is new or not.
			Punches.Value = result.Punches -- We set the data.
			Ball.Value = result.Ball
		else
			print("Player is new.") -- Player is new.
		end
	else
		warn("Error occured \n \n "..result) -- An error occured, log it.
	end
end

local function SaveData(player)
	local Data = {Ball = player.leaderstats.Ball.Value,Punches = player.leaderstats.Punches.Value} -- Our data table.
	local success,result = pcall(function() -- Function to prevent and see errors in a safe way.
		LeaderboardDataStore:SetAsync(player.UserId,Data) -- We save our data.
	end)
	if not success then
		warn("Error occured \n \n "..result) -- Log our error.
	end
end

Players.PlayerRemoving:Connect(SaveData)
Players.PlayerAdded:Connect(leaderboardSetup)
4 Likes

Doesn’t seem to work. Didn’t even log an error…

4 Likes
local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local LeaderboardDataStore = DataStore:GetDataStore("LeaderboardData") -- We get the datastore that being make one.

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Punches = Instance.new("IntValue")
	Punches.Name = "Smacks"
	Punches.Value = 0
	Punches.Parent = leaderstats

	local Ball = Instance.new("StringValue")
	Ball.Name = "Ball"
	Ball.Value = "Default"
	Ball.Parent = leaderstats

	local success,result = pcall(function() -- This function is use to prevent errors in console and to see if error did occur.
		return LeaderboardDataStore:GetAsync(player.UserId) -- We get our data.
	end)
	if success then -- We see if an error occurs or not.
		if result then -- We see if player is new or not.
			print("Loaded data!")
			Punches.Value = result.Punches -- We set the data.
			Ball.Value = result.Ball
		else
			print("Player is new.") -- Player is new.
		end
	else
		warn("Error occured \n \n "..result) -- An error occured, log it.
	end
end

local function SaveData(player)
	local Data = {Ball = player.leaderstats.Ball.Value,Punches = player.leaderstats.Punches.Value} -- Our data table.
	local success,result = pcall(function() -- Function to prevent and see errors in a safe way.
		LeaderboardDataStore:SetAsync(player.UserId,Data) -- We save our data.
	end)
	if success then
		print("Saved data!")
	else
		warn("Error occured \n \n "..result) -- Log our error.
	end
end

Players.PlayerRemoving:Connect(SaveData)
Players.PlayerAdded:Connect(leaderboardSetup)

I added pruints to let you know that it did work. Also you need to update the values on the server not on the client.

3 Likes

All the scripts aren’t local, it’s from a regular script

2 Likes

It also didn’t work. Is there a specific problem happening? All it says is “Player is new”, and when I rejoin all the “smacks” reset back to 0.
Edit: I’m going to try to use an alt to see if it was the button I was using causing problems
Edit again: Didn’t work aswell…

2 Likes

That is quite strange. Is it printing saved data?

2 Likes

No, I don’t see any printing.
If this helps: I put the leaderboard script inside of ServerScriptService

2 Likes

I recommend you use profile service it is a custom data service system it will allow your data to be way more secure than any other data system Use Profile Service as Your Data Store! Roblox Development - YouTube

2 Likes
local DataStore = game:GetService("DataStoreService")
local LeaderboardDS = DataStore:GetDataStore("Leaderboard")
local Players = game.Players
local RE = game.ReplicatedStorage.SetInfo

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

	local Punches = Instance.new("IntValue")
	Punches.Name = "Smacks"
	Punches.Value = 0
	Punches.Parent = leaderstats

	local Ball = Instance.new("StringValue")
	Ball.Name = "Ball"
	Ball.Value = "Default"
	Ball.Parent = leaderstats
	
	local suc,res = pcall(function()
		return LeaderboardDS:GetAsync(player.UserId)
	end)

	if suc then
		if res then
			Punches.Value = res.Punches
			Ball.Value = res.Ball
		end
	else
		warn("Error loading score for"..player.Name.."! \n \n "..res)
	end
end)

Players.PlayerRemoving:Connect(function(plr)
	local leaderstats = plr.leaderstats
	local data = {Punches = leaderstats.Punches.Value,Ball = leaderstats.Ball.Value}
	local suc,res = pcall(function()
		LeaderboardDS:SetAsync(plr.UserId,plr.HighScore.Value)
	end)
	if not suc then
		warn("Error saving score for "..plr.Name.."! \n \n "..res)
	end
end)

Here is a script I made using my script:

2 Likes

My leaderboard is now gone
image

2 Likes
local DataStore = game:GetService("DataStoreService")
local LeaderboardDS = DataStore:GetDataStore("Leaderboard")
local Players = game.Players
local RE = game.ReplicatedStorage.SetInfo

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

	local Punches = Instance.new("IntValue")
	Punches.Name = "Smacks"
	Punches.Value = 0
	Punches.Parent = leaderstats

	local Ball = Instance.new("StringValue")
	Ball.Name = "Ball"
	Ball.Value = "Default"
	Ball.Parent = leaderstats

	local suc,res = pcall(function()
		return LeaderboardDS:GetAsync(player.UserId)
	end)

	if suc then
		if res then
			Punches.Value = res.Punches
			Ball.Value = res.Ball
		end
	else
		warn("Error loading score for"..player.Name.."! \n \n "..res)
	end
end)

Players.PlayerRemoving:Connect(function(plr)
	local leaderstats = plr.leaderstats
	local data = {Punches = leaderstats.Smacks.Value,Ball = leaderstats.Ball.Value}
	local suc,res = pcall(function()
		LeaderboardDS:SetAsync(plr.UserId,data)
	end)
	if not suc then
		warn("Error saving score for "..plr.Name.."! \n \n "..res)
	end
end)

After some bug fixes… here you go.

1 Like

ProfileService is a wrap-around to the normal datastore. It adds a lot of protection and security, though I wouldn’t recommend it because it is quite advanced.

2 Likes

Ik but once you set it up, it is very easy to add new data etc.

2 Likes

Also didn’t work. Is there a specific issue happening?


Its “smacks” not punches
Punches.Name = "Smacks"
I have no clue how to fix this issue with datasaves though

2 Likes

Then just change it to Smacks. Not that hard.

1 Like

Sure but the user should also know the basics of datastore.

1 Like

Setting up is the hard part. It requires solid understanding of how to script in order to get what you want out of it (i.e., using a ModuleScript with set, get and vel sim functions.)