Greetings! Created my first data store that is fit to store alot of data, This works fine and i think its good but i may not be doing important stuff that could prevent data loss.
Please let me know how i could improve this code to make it save data better
Note: Data is saved every 5 minutes, when a player spends robux on dev product, and when the player leaves. Data is loaded every time the player joins
local DefaultEncoded = {0, true, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, "Right", 50, 50, false, false, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {}, "THIS IS A EMPTY SLOT FOR THE TIME THE PLAYER JOINED"}
local function Encode(Plr)
local Pets = {}
for i,v in pairs(Plr.OwnedPets:GetChildren()) do
table.insert(Pets, v.Value)
end
local EquipedPets = {}
for i,v in pairs(Plr.Equiped:GetChildren()) do
if v.Value then
table.insert(EquipedPets, tonumber(v.Value.Name:match("%d+")))
else
table.insert(EquipedPets, 0)
end
end
local Table = {Plr.leaderstats.Gold.Value, Plr.InvisStats.FirstTimer.Value, Plr.InvisStats.Xp.Value, Plr.InvisStats.PlayTime.Value, Plr.InvisStats.BestWorld.Value, Plr.InvisStats.CurrentWorld.Value, Plr.InvisStats.BestFrisbee.Value, Plr.InvisStats.EquipedFrisbee.Value, Plr.InvisStats.Rebirths.Value, Plr.InvisStats.StatStrengthLevel.Value, Plr.InvisStats.StatXpLevel.Value, Plr.InvisStats.StatCoinLevel.Value, Plr.InvisStats.RobuxSpent.Value, Plr.InvisStats.DailyRewardDay.Value, Plr.InvisStats.DailyRewardClaimedDay.Value, Plr.InvisStats.PlaytimeClaimedReward.Value, Plr.Settings.Handedness.Value, Plr.Settings.MusicVolume.Value, Plr.Settings.SfxVolume.Value, Plr.Settings.WalkSpeed.Value, Plr.Settings.GoldenFrisbee.Value, Pets, EquipedPets, Plr.InvisStats.JoinDate.Value}
local TheSame = true
for i,v in pairs(DefaultEncoded) do
if i ~= #Table then
if type(v) ~= "table" then
if Table[i] ~= v then
TheSame = false
end
else
for I,V in pairs(v) do
if Table[i][I] ~= V then
TheSame = false
end
end
end
end
end
if TheSame then
return false
end
return Table
end
local function Decode(Plr, Data)
local Pets = {}
for i,v in pairs(Plr.OwnedPets:GetChildren()) do
table.insert(Pets, v)
end
local PetsNames = {}
for i,v in pairs(game.ReplicatedStorage.Pets:GetChildren()) do
table.insert(PetsNames, v.Name)
end
table.sort(PetsNames, function(a, b)
return a < b
end)
local EquipedPets = {}
for i,v in pairs(Plr.Equiped:GetChildren()) do
table.insert(EquipedPets, v)
end
local Table = {Plr.leaderstats.Gold, Plr.InvisStats.FirstTimer, Plr.InvisStats.Xp, Plr.InvisStats.PlayTime, Plr.InvisStats.BestWorld, Plr.InvisStats.CurrentWorld, Plr.InvisStats.BestFrisbee, Plr.InvisStats.EquipedFrisbee, Plr.InvisStats.Rebirths, Plr.InvisStats.StatStrengthLevel, Plr.InvisStats.StatXpLevel, Plr.InvisStats.StatCoinLevel, Plr.InvisStats.RobuxSpent, Plr.InvisStats.DailyRewardDay, Plr.InvisStats.DailyRewardClaimedDay, Plr.InvisStats.PlaytimeClaimedReward, Plr.Settings.Handedness, Plr.Settings.MusicVolume, Plr.Settings.SfxVolume, Plr.Settings.WalkSpeed, Plr.Settings.GoldenFrisbee, Pets, EquipedPets, Plr.InvisStats.JoinDate}
for i,v in pairs(Table) do
if i ~= #Data then
if type(v) ~= "table" then
if Data[i] ~= v.Value then
v.Value = Data[i]
end
else
if #v == 3 then
for I,V in pairs(v) do
if Data[i][I] ~= 0 then
for o, p in pairs(Pets) do
if tonumber(p.Name:match("%d+")) == Data[i][I] then
V.Value = p
require(game.ReplicatedStorage.Modules.AddPets).AddPet(Plr, PetsNames[p.Value])
end
end
end
end
elseif #v == 50 then
for I,V in pairs(v) do
if Data[i][I] ~= V.Value then
V.Value = Data[i][I]
end
end
end
end
end
end
end
local DS = game:GetService("DataStoreService")
local Store = DS:GetDataStore("DataStore")
local DataStore = {}
function DataStore.SaveData(Plr)
if Plr.InvisStats.SaveData.Value then
local success, Error = pcall(function()
local EncodedData = Encode(Plr)
if EncodedData then
Store:SetAsync(Plr.UserId, EncodedData)
end
end)
if not success then
warn("Failed to save: ", Error)
end
end
end
function DataStore.LoadData(Plr)
local success, Error = pcall(function()
local EncodedData = Store:GetAsync(Plr.UserId)
if EncodedData then
Decode(Plr, EncodedData)
end
end)
if not success then
warn("Failed to load: ", Error)
Plr.InvisStats.SaveData.Value = false
Plr:Kick("We failed to save your data! Try again later.")
end
end
return DataStore
Thanks for any help!