How terrible is this datastore and any advice on making it better/cleaner ?
local dataStoreService = game:GetService("DataStoreService")
local leaderstatsDataStore = dataStoreService:GetGlobalDataStore("leaderstats")
local leaderboardDataStore = dataStoreService:GetGlobalDataStore("leaderboard")
local data = dataStoreService:GetDataStore("Stats")
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"
local leaderboard = Instance.new("Folder", Player)
leaderboard.Name = "leaderboard"
local Level = Instance.new("NumberValue", leaderstats)
Level.Name = "Level"
Level.Value = 1
local Exp = Instance.new("NumberValue", leaderboard)
Exp.Name = "Exp"
Exp.Value = 0
local RequiredEXP = Instance.new("NumberValue", Player)
RequiredEXP.Name = "RequiredExp"
RequiredEXP.Value = Level.Value * 100
local Wins = Instance.new("NumberValue", leaderstats)
Wins.Name = "Wins"
Wins.Value = 0
local savedLevel = leaderstatsDataStore:GetAsync(Player.UserId.."-Level")
if savedLevel then
Level.Value = savedLevel
end
--
local savedExp = leaderboardDataStore:GetAsync(Player.UserId.."-Exp")
if savedExp then
Exp.Value = savedExp
end
--
local savedRequiredEXP = leaderstatsDataStore:GetAsync(Player.UserId.."-RequiredEXP")
if savedRequiredEXP then
RequiredEXP.Value = savedRequiredEXP
end
--
local savedWins = leaderstatsDataStore:GetAsync(Player.UserId.."-Wins")
if savedWins then
Wins.Value = savedWins
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local sucess, err = pcall(function()
leaderstatsDataStore:SetAsync(plr.UserId.."-Level", plr.leaderstats.Level.Value)
end)
local sucess, err = pcall(function()
leaderstatsDataStore:SetAsync(plr.UserId.."-Wins", plr.leaderstats.Wins.Value)
end)
--
local sucess, err = pcall(function()
leaderboardDataStore:SetAsync(plr.UserId.."-Exp", plr.leaderboard.Exp.Value)
end)
--
local sucess, err = pcall(function()
data:SetAsync(plr.UserId.."-RequiredEXP", plr.RequiredEXP.Value)
end)
end)
````