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.