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