Quickly saving and loading huge chunk of data

I think I did it correctly, Sorry for the trouble. I’m just new to using datastores.

game.Players.PlayerRemoving:Connect(function(plr)

local plrInventory = char.Inventory:GetChildern()

for Index, Value in ipairs(plrInventory) do

local invData = game:GetService("DataStoreService"):GetDataStore("inventory-main")

local invDatakey = "data-"..plr.UserId

local GetSave = invData:GetAsync(invDatakey)

invData:SetAsync(invDatakey, Value.Value)

end

end)
  1. You don’t need to define invData when they leave, just put it on top of the script.

  2. I’m not too sure why you’re calling GetAsync() right before SetAsync, there is no need.

  3. Wrap your calls in a pcall (SetAsync & GetAsync).

After all, I still recommend using DataStore2, it is more reliable.

1 Like

Got a error.
attempted to index nil with ‘name’

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

Try putting just i instead of "Slot "..i

Something isnt right i think. It isnt saving. Clearly says its loading my inventory.

Code for saving.

game.Players.PlayerRemoving:Connect(function(plr)
local plrInventory = plr.Character.Inventory:GetChildern()
for Index, Value in ipairs(plrInventory) do
local invData = game:GetService("DataStoreService"):GetDataStore("inventory-main")
local invDatakey = "data-"..plr.UserId
pcall(function()
invData:SetAsync(invDatakey, Value.Value)
end)
end
end)

Are you JSON decoding when loading data?

Yes.

if GetSave then
        local dataToLoad = S_H:JSONDecode(GetSave)
        for i, item in pairs(dataToLoad) do
        addItem(inv, dataToLoad[i].name, dataToLoad[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!")

I do see a error Callbacks cannot yield

Then you should be also JSON encoding when saving data.

Yeah I just done that and it still won’t work.

Leaving:

game.Players.PlayerRemoving:Connect(function(plr)
local plrInventory = plr.Character.Inventory:GetChildern()
local data = {}
for Index, Value in ipairs(plrInventory) do
local stuff = 
{
[Value.Name] = {Value.Value}
}
table.insert(data, 1, stuff)
end
pcall(function()
local invData = game:GetService("DataStoreService"):GetDataStore("inventory-test2")
local invDatakey = "data-"..plr.UserId
local encodedData = S_H:JSONEncode(data)
invData:SetAsync(invDatakey, encodedData)
end)
end)

Joining:

 local inv = getInventory(char)
 local invData = game:GetService("DataStoreService"):GetDataStore("inventory-test2")
    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[i].name, dataToLoad[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)
        pcall(function()
        invData:SetAsync(invDatakey, dataToSave) 
        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)
        print("Data saved for "..plr.Name.."'s inventory loaded!")
    end

I think it’s a saving problem or loading.

this is what im getting, could be unrealeated towards the saving/loading tho.

Didn’t you have count value too?

The Value name in the folder is Slot 1, Slot 2, and so on.

Yes, I know, I am talking about count as in amount of a specific item

The value is a json value.
{“name”:“Dirt”,“count”:1}

First of all, declare the variable invData at the top of your script. Secondly, try putting invDatakey and encodedData outside the pcall.

Putting local invDatakey = “data-”…plr.UserId
local encodedData = S_H:JSONEncode(data) at top of pcall won’t do anything, I did move invData to top of the script and testing it right now.