Should I merge Leaderboard and DataStore scripts?

Hi, I recently started working with Data Stores and I’m using them for my game.
The game also has a leaderboard and thus needs to get the values from DataBase to display them on the leaderboard.
I currently have two different scripts to manage Data Stores and leaderboards.

Here is the Data Store script
local DataStoreService = game:GetService("DataStoreService")
local checkpointStore = DataStoreService:GetDataStore("PlayerCheckpoint")
local moneyStore = DataStoreService:GetDataStore("MoneyStore")
local winStore = DataStoreService:GetDataStore("WinStore")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	-- get checkpoint Data
	local success, currentCheckpoint = pcall(function()
		return checkpointStore:GetAsync(player)
	end)
	local checkPoint = Instance.new("IntValue", player)
	checkPoint.Name = "CheckPoint"
	if success then
		print("Current Checkpoint:", currentCheckpoint)
		checkPoint.Value = currentCheckpoint
	else
		print ("Checkpoint initialized")
		checkPoint.Value = 2
	end
	
	-- get money Data
	local success, currentMoney = pcall(function()
		return moneyStore:GetAsync(player)
	end)
	local money = Instance.new("IntValue", player)
	money.Name = "Money"
	if success then
		print("Current Money:", currentMoney)
		money.Value = currentMoney
	else
		print("Money initialized")
		money.Value = 0
	end
	
	-- get wins Data
	local success, currentWins = pcall(function()
		return winStore:GetAsync(player)
	end)
	local wins = Instance.new("IntValue", player)
	wins.Name = "Wins"
	if success then
		print("Current Wins:", currentWins)
		wins.Value = currentWins
	else
		print("Wins initialized")
		wins.Value = 0
	end
end)

Players.PlayerRemoving:Connect(function(player)
	-- set checkpoint Data
	local playerCheckpoint = player.CheckPoint.Value
	local success, err = pcall(function()
		checkpointStore:SetAsync(player, playerCheckpoint)
	end)
	if success then
		print("Checkpoint save Success")
	else
		print("Checkpoint save Error")
	end
	
	-- set money Data
	local playerMoney = player.Money.Value
	local success, err = pcall(function()
		moneyStore:SetAsync(player, playerMoney)
	end)
	if success then
		print("Money save Success")
	else
		print("Money save Error")
	end
	
	-- set wins Data
	local playerWins = player.Wins.Value
	local success, err = pcall(function()
		winStore:SetAsync(player, playerWins)
	end)
	if success then
		print("Wins save Success")
	else
		print("Wins save Error")
	end
end)

Here is the Leaderboard script
local Players = game:GetService("Players")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	-- create the money variable
	local money = Instance.new("IntValue")
	money.Name = "Coins"
	money.Value = player:WaitForChild("Money").Value
	money.Parent = leaderstats
	
	-- create the checkpoint variable
	local checkpoint = Instance.new("IntValue")
	checkpoint.Name = "Checkpoint"
	checkpoint.Value = player:WaitForChild("CheckPoint").Value
	checkpoint.Parent = leaderstats
	
	-- create the wins variable
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = player:WaitForChild("Wins").Value
	wins.Parent =leaderstats
end

Players.PlayerAdded:Connect(leaderboardSetup)

As you can see, 3 values are used. This is working completely fine but here is my question:
These scripts both create a new variable for each value and I’m wondering if it is better for me to try and merge those scripts so that the player only has each value once. (Only in the leaderstats folder then).
I’m not an experienced scripter. That’s why I’m looking for other’s opinions.

English isn’t my first language, I hope you can understand everything. Tell me if something isn’t clear enough.

I think it would best you merge them as that is how most people do it and is more organized than making separate scripts imo.

Also I think it would better to use a single datastore and give it an Encoded/Decoded table of information instead of having 3 datastores, would remove clutter

1 Like

Thank you for your reply.

What do you mean here? Sorry I’m new to Data Store.

What I meant by that is to store all the things you want in a dictionary, such as

local tbl = {
    money = player.leaderstats.Money.Value
    kills = player.leaderstats.Kills.Value
}

And then you set that as the value in their datastore via encoding it using HTTPService:JSONEncode, which is a function to turn a table into an encoded json string, which is perfect for what want to do, you set the result of it in the player’s key, example

local result = HTTPService:JSONEncode(tbl)
DataStore:SetAsync(player,result)

Where HTTPService is the name of the variable that contains that service and DataStore is the name of the varaible that contains our datastore, and to retrieve the data back as a table, you just use HTTPService:JSONDecode

local returned = HTTPService:JSONDecode(DataStore:GetAsync(player)
print(returned.money)

To obtain the money of that player

Thank’s again. I’ll be honest, I din’t really understand this but it’s fine. I’ll keep using my 3 data stores as it’s working fine.
I’ll manage to merge my scripts though. It will be more organized and I won’t have duplicated variables.

Thank you for helping, have a nice day!
RID3R

1 Like

Anytime! And yea, if it works for you, it’s okay, personal preference, but at some point I would recommend learning how to do it with a dictionary to help reduce clutter

If you have anymore issues don’t be afraid to make another post!

1 Like