Help with DataStore2 tables

Hello,

So I am making an inventory system that saves using the DataStore2 module. I have a server script that has bindable/remote events/functions that manipulate the DataStore content. In my case, the inventory is a table stored in the datastore, so all inventory manipulation such as adding items, removing, and updating items is done directly into the datastore.

The problem is that I am unsure how to use the table.insert() function to actually insert into the DataStore table (In another word, putting a datastore method as the first parameter in table.insert()). What first got in my mind is to instead of adding a new item and removing it, I would have to make a separate module or function that actually adds the new item to a temporary table and then switch the whole datastore table out with it.

Here is the code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local modules = ReplicatedStorage:WaitForChild("Modules")
local events = ReplicatedStorage:WaitForChild("Events")
local equipment = events:WaitForChild("Equipment")

local DataStore2 = require(modules:WaitForChild("DataStore2"))
local GameItems = require(modules:WaitForChild("GameItems"))

local FromServer = equipment:WaitForChild("FromServer")
local FromClient = equipment:WaitForChild("FromClient")

local AddItemEvent = FromServer:WaitForChild("AddItem")
local GetItems = FromClient:WaitForChild("GetItems")

local defaultValue = {GameItems["Wooden Sword"]}

Players.PlayerAdded:Connect(function(player)
	-- Check if the player has inventory data in the datastore
	local ItemsDataStore = DataStore2("Items", player)
	
	local function itemsUpdate(updateValue)
		local itemDS = ItemsDataStore:Get(updateValue)
	end
	
	itemsUpdate(defaultValue)
	
	ItemsDataStore:OnUpdate(itemsUpdate)
	
	
	
	GetItems.OnServerInvoke = function()
		local items = ItemsDataStore:Get(defaultValue)
		return items
	end
	
	AddItemEvent.Event:Connect(function(item)
		-- This is where I dont know what to do... perhaps table.insert()
	end)
	
end)

Thank you in advance :smile:

1 Like

you just alter data in the cached table, then save the entire data to the store.

  local cached = Store:GetTable(defaultStats)
  cached.Currency = cached.Currency + 10 -- just alter a copy of the saved data

  Store:Set(cache)

Thank you! However, this is how the cashed table look:

{
	["Wooden Sword"] = {
		["Rarity"] = 1,
		["Damage"] = 10,
		["Price"] = 100
	},
}

Now, when actually adding a new “item” (which is the table inside the table), how would i go forward to actually add a new one? Would this work?

  local cached = Store:GetTable(defaultStats)
  table.insert(cached, item) -- where item is a variable that hold the table

  Store:Set(cache)

Sorry for the late reply,

table.insert along with most table functions are irrelevant to dictionaries, if you need to set a new index where the table would have non-numerical indices,

local cached = Store:GetTable(defaultStats)
cached["new sword"] = {

            ["Rarity"] = 1, 
   		    ["Damage"] = 10,
		    ["Price"] = 100
}

Store:Set(cached)