The title is confusing, basically, the LoadData function will attempt to write data to values inside of a folder. The thing is, the game uses places, and certain data is not useful in place one, so i don’t want to bother to add the value there too. I tried using UpdateAsync but i didn’t understand how it worked originally. anyways, here’s the code (kinda messy)
local DataStore = game:GetService("DataStoreService"):GetDataStore("Encore")
local CanSave = false
local function SaveData(plr)
if CanSave then
local Data = {}
for _, Values in pairs(workspace.Technical.DataValues:GetDescendants()) do
Data[Values.Name] = Values.Value
print(Values.Value)
end
print("save:", Data)
DataStore:SetAsync(plr.UserId, Data)
end
end
local function LoadData(plr)
return DataStore:GetAsync(plr.UserId)
end
--//loading data
game.Players.PlayerAdded:Connect(function(plr)
local success, data = pcall(function()
return LoadData(plr)
end)
print(success)
print("load:", data)
if not success then
CanSave = false
local tries = 0
while task.wait() do
if tries <= 10 then
plr:Kick("All attempts to load data were unsuccessful. Please rejoin or try again later.")
break
else
tries += 1
success, data = pcall(function()
return LoadData(plr)
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 succ, err = pcall(function()
val.Value = data[val.Name]
print(val.Value)
end)
if not succ then
warn("Key not found or mismatched values. Luau error:", err)
end
end
end
end)
--//saving data
game.Players.PlayerRemoving:Connect(function(plr)
local success, errormsg = pcall(function()
SaveData(plr)
end)
end)
game:BindToClose(function()
for _, plr in pairs(game.Players:GetPlayers()) do
local success, errormsg = pcall(function()
SaveData(plr)
end)
end
end)
--task.spawn(function()
-- while task.wait() do
-- print(CanSave)
-- end
--end)