Okay- I am very much new to scripting, so there is a chance that the issue is very obvious. The code below -works-, as in, almost all of it but one single line.
local serverScriptService = game:GetService("ServerScriptService") local dataStoreService = game:GetService("DataStoreService") local httpService = game:GetService("HttpService") local dataValues = script.dataValues local serverModules = serverScriptService.serverModules local dataStore = dataStoreService:GetDataStore("player_Data2") --Load Data game.Players.PlayerAdded:Connect(function(plr) local success, err = pcall(function() local key = "ID-".. tostring(plr.UserId) local getSave = dataStore:GetAsync(key) local dataFolder = Instance.new("Folder", plr) dataFolder.Name = "dataFolder" --Creates a copy of all datavalues and pastes them into DataFolder. It also makes sure each player has updated datavalues if I add a new one. for i, v in pairs(dataValues:GetChildren()) do local newData = Instance.new(v.ClassName) newData.Name = v.Name newData.Value = v.Value newData.Parent = dataFolder end --Check if the player already has data if getSave then --Has data, so we simply load the saved values into the values in the dataFolder for i, v in pairs(dataValues:GetChildren()) do local decodedData = getSave dataFolder[v.Name].Value = decodedData[i] print(i, v.Name, v.Value) end else --Does not have data, so we create an array of data for them. local saveData = {} for i, v in pairs(dataFolder:GetChildren()) do table.insert(saveData, v.Value) print(i, v.Name, v.Value) end local encodedData = httpService:JSONEncode(saveData) dataStore:SetAsync(key, encodedData) end end) if err then print("Error Loading Data.") end end) --Save Data game.Players.PlayerRemoving:Connect(function(plr) local success, err = pcall(function() local dataFolder = plr:WaitForChild("dataFolder") local dataList = dataFolder:GetChildren() local key = "ID-".. tostring(plr.UserId) local saveData = {} for i, v in pairs(dataList) do table.insert(saveData, v.Value) print(i, v.Name, v.Value) end local encodedData = httpService:JSONEncode(saveData) dataStore:SetAsync(key, encodedData) end) if err then print("Error Saving Data.") end end)
The issue comes in game.Players.PlayerRemoving... Specifically this line, " dataStore:SetAsync(key, encodedData)". Everything up to that point works, just for some reason it doesn't save the data, albeit, when the player joins, and has no data, the same code does work. So I am confused about this.
Never mind guys! I actually found out how to fix it myself. The new code is documented below, albeit with some other changes, (other errors that I found in the here and there that I didn't notice yet) Basically, removing the old data first before then setting the new data is what I needed to do.
local serverScriptService = game:GetService("ServerScriptService") local dataStoreService = game:GetService("DataStoreService") local httpService = game:GetService("HttpService") local dataValues = script.dataValues local serverModules = serverScriptService.serverModules local dataStore = dataStoreService:GetDataStore("player_Data2") --Load Data game:GetService("Players").PlayerAdded:Connect(function(plr) local success, err = pcall(function() local key = "ID-".. tostring(plr.UserId) local getSave = dataStore:GetAsync(key) local dataFolder = Instance.new("Folder", plr) dataFolder.Name = "dataFolder" --Creates a copy of all datavalues and pastes them into DataFolder. It also makes sure each player has updated datavalues if I add a new one. for i, v in pairs(dataValues:GetChildren()) do local newData = Instance.new(v.ClassName) newData.Name = v.Name newData.Value = v.Value newData.Parent = dataFolder end --Check if the player already has data if getSave then --Has data, so we simply load the saved values into the values in the dataFolder local dataList = dataFolder:GetChildren() for i, v in pairs(dataList) do local decodedData = httpService:JSONDecode(getSave) v.Value = decodedData[i] print(decodedData[i]) end print(dataStore:GetAsync(key)) else --Does not have data, so we create an array of data for them. local saveData = {} for i, v in pairs(dataFolder:GetChildren()) do table.insert(saveData, v.Value) end local encodedData = httpService:JSONEncode(saveData) dataStore:SetAsync(key, encodedData) end end) if err then print("Error Loading Data.") end end) --Save Data game:GetService("Players").PlayerRemoving:Connect(function(plr) local success, err = pcall(function() local dataFolder = plr:WaitForChild("dataFolder") local dataList = dataFolder:GetChildren() local key = "ID-".. tostring(plr.UserId) dataStore:RemoveAsync(key) local saveData = {} for i, v in pairs(dataList) do table.insert(saveData, v.Value) print(i, v.Name, v.Value) end local encodedData = httpService:JSONEncode(saveData) dataStore:SetAsync(key, encodedData) end) if err then print("Error Saving Data.") end end)