Quickly saving and loading huge chunk of data

I feel or may be doing this code completely wrong, but. So Instead of me pasting addItem() 103+ times, is there a quicker way to do that in a few lines of code and save that much data. Heres so far on my code which forces me to paste addItem() 103 times. I also have no idea how to quickly save it all when the player leaves.

local inv = getInventory(char)
    local invData = game:GetService("DataStoreService"):GetDataStore("inventory-main")
    local invDatakey = "data-"..plr.UserId
    local GetSave = invData:GetAsync(invDatakey)
    if GetSave then
        local dataToLoad = S_H:JSONDecode(GetSave)
        addItem(inv,dataToLoad["Slot 1"].name, dataToLoad["Slot 1"].count)
        local alert = script.alert:Clone()
        alert.alert.Text = "Successfully Loaded your inventory from last gameplay save!"
        alert.Parent = plr.PlayerGui.HUDGui.Notification
        print("Data saved for "..plr.Name.."'s inventory loaded!")
    else
        local alert = script.alert:Clone()
        alert.alert.Text = "No data saved, loading default data"
        alert.Parent = plr.PlayerGui.HUDGui.Notification
        local newData = {
         ["Slot 1"] = {
          name = "GrassBlock";
          count = 1	
          }
        }
        local dataToSave = S_H:JSONEncode(newData)
        invData:SetAsync(invDatakey, newData)
        local alert = script.alert:Clone()
        alert.alert.Text = "Successfully Loaded your inventory from last gameplay save!"
        alert.Parent = plr.PlayerGui.HUDGui.Notification
        print("Data Saved for ".. plr.Name)
        print("Data saved for "..plr.Name.."'s inventory loaded!")
    end
2 Likes

Personally, I use JSONEncode to group together a bunch of data. I use DataStore2 to save the data. Although Encoding it in JSON is not required, I like doing so.

1 Like

Alright, still doesnt solve my issue saving & loading without pasting a line of code 103 times.

1 Like

To make more objects/instances to the datastore?

2 Likes

In-game player has aprox 103+ plus slots, the data is stored in a folder with string values json encoded, When player leaves I need to save that data, also when the player connects I need to load the data.

for i,v in pairs(datafolder:GetChildren()do
local slot = v.Slot
end

You could use this loop to loop through all the slots, then get the datastore based on the slot data.

You can use a for loop.

for i, item in pairs(dataToLoad) do
    addItem(inv, dataToLoad["Slot "..i].name, dataToLoad["Slot "..i].count)
end
1 Like

Is there a way to actually just save & load the folder?

No, there isn’t, you would have to run the addItem() function for each item. (in a for loop of course)

My brain hurts looking at this code trying to load & save the data.

local inv = getInventory(char)
 local invData = game:GetService("DataStoreService"):GetDataStore("inventory-main")
    local invDatakey = "data-"..plr.UserId
    local GetSave = invData:GetAsync(invDatakey)
    if GetSave then
        local dataToLoad = S_H:JSONDecode(GetSave)
        for i, item in pairs(dataToLoad) do
        addItem(inv, dataToLoad["Slot "..i].name, dataToLoad["Slot "..i].count)
        end
        local alert = script.alert:Clone()
        alert.alert.Text = "Successfully Loaded your inventory from last gameplay save!"
        alert.Parent = plr.PlayerGui.HUDGui.Notification
        print("Data saved for "..plr.Name.."'s inventory loaded!")
    else
        local alert = script.alert:Clone()
        alert.alert.Text = "No data saved, loading default data"
        alert.Parent = plr.PlayerGui.HUDGui.Notification
        local newData = {
         ["Slot 1"] = {
          name = "GrassBlock";
          count = 1	
          }
        }
        local dataToSave = S_H:JSONEncode(newData)
        invData:SetAsync(invDatakey, dataToSave)
        local alert = script.alert:Clone()
        alert.alert.Text = "Successfully Loaded your inventory from last gameplay save!"
        alert.Parent = plr.PlayerGui.HUDGui.Notification
        print("Data Saved for ".. plr.Name)
        print("Data saved for "..plr.Name.."'s inventory loaded!")
    end

Plus it’s 1 am :confused:

Data in the values in the folder is json.

{“name”:“Dirt”,“count”:15}

That’s the only way I can think of right now. Does it not work?

i isnt identifed in the script.

i automatically gets defined by the for loop.

I mean, It’s suppose to loop only 103 times.

You don’t even need JSONDecode/Encode in this situation.
You’re saving a regular table.

I would recommend DataStore2 though.
Use :GetTable(Table) when you need the data, and :Set(Table) to update its cache.

You can also look into :Save and :SetAsync() for further information.

1 Like

Wait I misread, I’m so tired. How would I save all the data in the folder.

If the player has 5 items in his inventory, it will loop 5 times. If you want it to loop 103 times, you can do:

for i = 1,103,1 do
    addItem(inv, dataToLoad["Slot "..i].name, dataToLoad["Slot "..i].count)
end
local GetChildren = Folder:GetChildren()
for Index, Value in ipairs(GetChildren) do
 --- save.
end

This is the basic, I think you can figure out the rest yourself.

The method @Syclya suggested is the best one (and the most efficient one) and would fit the situation perfectly.