I made it so that if a value is missing in a place, it won’t get erased (it’s difficult to explain) but it seems like it made it so that values don’t update (they do get added to the table, but with the old values)
--!native
local DataStore = game:GetService("DataStoreService"):GetDataStore("Encore")
local CanSave = false
local function SaveData(Player)
if CanSave then
local Data = {}
for _, Values in pairs(workspace.Technical.DataValues:GetDescendants()) do
Data[Values.Name] = Values.Value
--print(Values.Value, 1)
end
DataStore:UpdateAsync(Player.UserId, function(OldData)
if OldData ~= nil then
for index, val in pairs(OldData) do
if not table.find(Data, index) then
--print(val, 2)
Data[index] = val
end
end
end
print("Save:", Data)
return Data
end)
end
end
local function LoadData(Player)
return DataStore:GetAsync(Player.UserId)
end
--//loading data
game.Players.PlayerAdded:Connect(function(Player)
local success, data = pcall(function()
return LoadData(Player)
end)
print(success)
print("Load:", data)
if not success then
CanSave = false
for tries = 1, 10 do
if tries <= 10 then
Player:Kick("All attempts to load data were unsuccessful. Please rejoin or try again later.")
else
tries += 1
success, data = pcall(function()
return LoadData(Player)
end)
if success then
CanSave = true
print("Loaded")
break
end
end
end
else
CanSave = true
end
if not data then
data = {}
else
for _, val in pairs(workspace.Technical.DataValues:GetDescendants()) do
local success, err = pcall(function()
val.Value = data[val.Name]
end)
if not success then
warn("Key not found or mismatched values. Luau error:", err)
end
end
workspace.Technical.DataValues.DescendantAdded:Connect(function(descendant)
local success, err = pcall(function()
descendant.Value = data[descendant.Name]
end)
if not success then
warn(`Mismatched values! Luau error: {err}`)
end
end)
end
end)
--//saving data
game.Players.PlayerRemoving:Connect(function(Player)
local success, errormsg = pcall(function()
SaveData(Player)
end)
end)
game:BindToClose(function()
for _, Player in pairs(game.Players:GetPlayers()) do
local success, errormsg = pcall(function()
SaveData(Player)
end)
end
end)
--//debugging
--task.spawn(function()
-- while task.wait() do
-- print(CanSave)
-- end
--end)