Ok so now it saves and loads. However when it saves According to my print statements the table is as it should be Then it saves. Now it loads however its loading with the default??? So then I did a print check on the actual player data from getasync. The data itself is showing defaults which is VERY confusing. I honestly don’t even know where this logic error is and I’ve had over 9 people look at this code and every single one of them couldn’t notice any problems with my code.
Sorry for whoevers going to read this although it’s slightly lengthy. Firstly we have the “DataRetrieve”. Should be properly renamed to DataSettingUp tbh. Essentially this script will retrieve any data and if this players data is not nil then it will apply their data
function module.DataRetrieve(Player,data)
local dataKey = nil
local player_data = nil
local Tries = 0
local Failed_Loading = false
repeat
local s,response = pcall(function()
dataKey = "data-"..tostring(Player.UserId)
player_data = data:GetAsync(dataKey)
end)
if s then
Tries = 3
end
if response then
Tries = Tries + 1
warn("Retrying data load for "..Player.UserId)
warn(response)
if Tries == 3 then
Failed_Loading = true
end
end
until Tries == 3 or s
if Failed_Loading == true then
Player:Kick("Error 1, Data failed to load for you. Please rejoin")
end
local TemplateData = serverStorage.TemplateData:Clone()
TemplateData.Parent = replicatedStorage.PlayerData
TemplateData.Name = Player.UserId
local Folder = nil
--[[
-- check to see what data exists
for i,v in pairs(player_data) do
print(i.." 59")
for j,k in pairs(v) do
print(j.." 61")
for _,value in pairs(k) do
print(value.." 63")
end
end
end
]]
local s,response = pcall(function()
if player_data ~= nil then
for i,v in ipairs(TemplateData:GetChildren()) do
--for i,v in pairs(player_data) do
if v:IsA("Folder") then-- means its a table in a table
for index,data in pairs(player_data) do
for folderName,Objects in pairs(data) do
if v.Name == folderName then
warn("Found Match")
for ObjectName,ObjectValue in pairs(Objects) do
print("81 "..ObjectName.." "..ObjectValue)
for r,ActualInstance in ipairs(v:GetChildren()) do
if ActualInstance.Name == ObjectName then
warn("Found Match in instances. editing")
ActualInstance.Value = ObjectValue
end
end
end
end
end
end
end
end
warn("Finished Loading data for "..Player.Name)
else
warn("Player has no data. Will keep with the defaults")
end
end)
if response then
warn(response)
end
end
Now moving onto what saves when you leave the game.
function module.DataSave(Player,Data)
local dataKey = "data-"..tostring(Player.UserId)
local player_data = nil
local Tries = 0
local Failed_Save = false
local InstanceData = replicatedStorage.PlayerData:FindFirstChild(Player.UserId)
local dataToSave = {}
local dataTable = {}
for i,v in ipairs(InstanceData:GetChildren()) do
if v:IsA("Folder") then
for _,obj in ipairs(v:GetChildren()) do
dataTable = {[v.Name] = {[obj.Name] = obj.Value}}
end
end
end
table.insert(dataToSave,dataTable)
repeat
local s,response = pcall(function()
Data:SetAsync(dataKey,dataToSave)
end)
if s then
Tries = 3
warn(Player.Name.."'s Data has saved")
end
if response then
Tries = Tries + 1
wait(1)
if Tries == 3 then
Failed_Save = true
warn(Player.Name.."'s Data has failed to save.")
end
end
until Tries == 3 or s
end
A few notes that may help. These are scripts in a module (Obviously). So here’s the scripts code incase this is connected.
local data = DataStoreService:GetDataStore(Key.Value.."Data")
DataModule.Initilize()
Player_Table = {}
Players.PlayerAdded:Connect(function(Player)
DataModule.DataRetrieve(Player,data) -- Setting up / Loading data
wait()
if Players:FindFirstChild(Player.Name) then -- Checking to make sure they didn't get kicked for data not loading
--table.insert(Player_Table,Player) -- Adding player to autosave queue
end
end)
Players.PlayerRemoving:Connect(function(Player)
local InstanceData = replicatedStorage.PlayerData:FindFirstChild(Player.UserId)
print("wag1 g running the exact same print check from script lets see.. - "..replicatedStorage.PlayerData:FindFirstChild(Player.UserId).Data.AValue.Value)
DataModule.DataSave(Player,data) -- Saving
--table.remove(Player_Table,Player) -- Removing from auto-save queue
end)
Now for more context on what this is actually doing.
So basically in serverstorage I have this:
The code will practically clone this and rename templatedata to their UserId. Then on player leaving it will copy all the data in the folder like this: For folders it will just save the name, For actual values it saves name and value. Then in the loading function it simply does a matching process to find out what var = instance name. Let me know if you need any more information as this is really starting to frustrate me. You can also see why I want this to work badly as a visual ds system is very very useful since I can easily add any new data values or remove them without any issues and its just ease-of-use.