Hello, I'm trying to have a re-attempt at my saving script to try make it more efficient. To do this i want to minimise the amount of data storing happening in the servers meaning storing a table under one Data store.
I have heard it is possible and therefore would love to get this working. I have attempted to do this and have failed. If anyone could point me in the right direction i would really appreciate it. Thanks.
Heres a snippet of the code which attempts at saving/loading a table:
local datastore = game:GetService("DataStoreService")
--Saving Variables here
local ds1 = datastore:GetDataStore("Data")
-----------------------------------------------------
game.Players.PlayerAdded:connect(function(plr)
local folder = Instance.new("Folder", plr) -- Creates a folder in the player
folder.Name = "Data" -- Having the name other than 'leaderstats' will make all values private and therefore other players will not see the stats.
local leaderstat = Instance.new("Folder", plr)
leaderstat.Name = "leaderstats"
--If making a new stat to save, please also define it here.
--------------------------------------------------------------------
local Level = Instance.new("IntValue", folder)-- Creating the Level
Level.Name = "Level"
local Experience = Instance.new("IntValue", folder)-- Creating the Xp
Experience.Name = "Xp"
local Currency = Instance.new("IntValue", folder)-- Creating the Currency
Currency.Name = "DefenderCoins"
local Dir = {Level.Value,Experience.Value,Currency.Value}
---------------------------------------------------------------------
local Data = plr:FindFirstChild("Data")
local data = ds1:GetAsync(plr.UserId) or nil --Loads in player data then saves however if no data is found then data will be reset to 0
if data == nil then
Data:WaitForChild("Level").Value = 1
ds1:SetAsync(plr.UserId, Dir)
data = ds1:GetAsync(plr.UserId) or nil
end
Level.Value = data[1]
Experience.Value = data[2]
Currency.Value = data[3]
--[[ --THIS WHILE LOOP IS JUST FOR TESTING THE SAVE
while true do wait(20) -- Autosaver
ds1:SetAsync(plr.UserId, Dir)
print("Data Auto Saved")
end
--]]
end)
game.Players.PlayerRemoving:Connect(function(plr)
ds1:SetAsync(plr.UserId, {plr.Data.Level.Value,plr.Data.Xp.Value,plr.Data.DefenderCoins.Value})
print("Data Saved")
end)
P.S: Original saving script works but i had edited that script to make this to try save and load a table. No errors pop up so its must be a logic error.

7 comments
Now that you've updated it, start the process of debugging code by setting up print'1', 2 and so forth around your code. I'd give the the answer straight up but this is a FM, though you are making an attempt at learning. Set up the print()'s, then see where it stops and go from there! Keep updatingthe question
It passes through the whole script. It just doesn't like the table. ANd i've tried printing out the table and the values show 0 even though in test mode i've changed them to try save it. So what i may try to do is manually put the directory for the values instead of using the cloned directories.
Thankyou very much for your assistance and for aiding me through it rather than giving me the code. Much appreciated :)
The issue was that local test mode still doesn't allow for the player to change data so i had to goo into the local server test mode and change the values via the server. it works ^_^
Glad I could help! I thought you were testing this using Test > Start Server, so I was a bit confused heh. Glad you figured out why it wouldn't save after is passed through the whole script! That's part of the process of debugging, and you're getting better at it already. I do prefer to give an outline of the code rather than the answer, to see if you can translate what I said in English into code
Also, here's a quick wiki on spawn: https://www.robloxdev.com/articles/Built-in-Functions-and-Variables/Roblox#spawn It, in this case, would allow the while loop to exist, while the code still passing through. I don't think you need it here, though good to know in the future. You can roam the SH site for questions involving spawn()
Thats convenient. So essentially 2 sections of the code is running at once. I'll keep that in mind :)