-
**What do you want to achieve? Keep it simple and clear!
I want this datastore to eventually hold at least 40+ values, with minimal to no data loss.
I know I need to implement tables for that but honestly, I don’t know what the best way is to do so. -
**What is the issue?
There is no issue yet, I just want to know what the best way is to improve this. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve asked several social media servers. But none of them really helped.
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local saveDataStore = dataStoreService:GetDataStore("DataStore11")
players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Parent = leaderstats
local DataId = Instance.new("IntValue")
DataId.Value = 0
DataId.Parent = plr
local data = saveDataStore:GetAsync(plr.UserId)
if data then
for name,value in pairs(data.leaderstats) do
leaderstats[name].Value = value
end
end
end)
players.PlayerRemoving:Connect(function(plr)
local saveData = {leaderstats = {}}
for _,stat in pairs(plr.leaderstats:GetChildren()) do
saveData.leaderstats[stat.Name] = stat.Value
if saveData then
saveDataStore:UpdateAsync(plr.UserId, function(oldValue)
local PreviousData = oldValue or {DataId = 0}
if saveData.DataId == PreviousData.DataId then
saveData.DataId = saveData.DataId + 1
return saveData
else
return nil
end
end)
end
end
end)
local RunService = game:GetService("RunService")
if not RunService:IsStudio() then
game:BindToClose(function()
for _,plr in pairs(players:GetChildren()) do
if #game[plr] <= 1 then
local saveData = {leaderstats = {}}
for _,stat in pairs(plr.leaderstats:GetChildren()) do
local success, err = pcall(function()
saveData.leaderstats[stat.Name] = stat.Value
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end
end
end)
end```