Is it possible to save a table through data store like this?

Hello. I am making an inventory, and I am looking to save a player’s inventory whenever he leaves under a table. Will that work:

local module = require(script.Inventory);

local inventoryDataStore = dataStoreService:GetDataStore("InventoryDataStore");

local function player_Removing(player)
	local data = inventoryDataStore:GetAsync(player.UserId);
	
	if data then
		inventoryDataStore:UpdateAsync(inventoryDataStore, function() -- There was an already existing key, so I didn't create a new key rather updated the existing one.
			return module:GetInventory(player)
		end)
	else
		local newDataStore = inventoryDataStore:SetAsync(player.UserId, nil); -- Creating a new data store key.
		
		inventoryDataStore:UpdateAsync(inventoryDataStore, function() -- Updating the existing data store key.
			return module:GetInventory(player)
		end)
	end
end;

players.PlayerRemoving:Connect(player_Removing)

Yes, you are able to store a table through a data store. :smiley:

Will the code that I provided work?

I don’t know. Are there any errors?

I haven’t tested it yet. Look at my code and tell me your opinion

we have no idea what’s in that module… what does “module:GetInventory” do?

it’s probably better to just test it, then ask for help if you get errors

Yes, I can’t tell you what will and will not happen more precisely unless you run the code first, and show us the results. If you want to know “if it will work or not” that’s the only thing we can recommend you do in this situation.

1 Like