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.
I'll give you the logic behind this, rather than the code. Of course if you still need help, just ask. Here's how the code should work:
data
variable to equal the new saved data (which is {1, 0,0})