Issues Saving Inventory with DataStore2

Hi! I am trying to write a system to convert an inventory folder in the player into a table with DataStore2. That table would obviously then be saved and loaded when needed. However, I am not getting the desired result. A value is being put in the users’ inventory, but with no name.

EDIT - Revamped code, still getting an error, provided more information.

Here is the error -

  10:56:18.537 - ServerScriptService.InventorySavingHandler:25: bad argument #3 (string expected, got table) 

Here is my datastore code -

local InventorySavingHandler = {}

local DataStore2 = require(script.Parent.DataStore2)

local defaultValue = {}
local invTable = {}

game.Players.PlayerAdded:Connect(function(plr)
	
	DataStore2.Combine("DATA", "inventory")
	
	local inventoryDataStore = DataStore2("inventory", plr)
	
	inventoryDataStore:SetBackup(5)
	local inventory = plr:WaitForChild("Inventory")
	local inventoryTable = inventory:GetChildren()
	
	local function inventoryUpdate(updateValue)
		
		defaultValue = invTable
		
	end
	
	inventoryUpdate(defaultValue)
	inventoryDataStore:OnUpdate(inventoryUpdate)
	
end)

function InventorySavingHandler.AddItem(player, item)
	
	local InventoryStore = DataStore2("inventory", player)
	print("Working with " .. item)
	table.insert(invTable, item)
	defaultValue = invTable
	
end

return InventorySavingHandler


Where additem is called-

event.OnServerEvent:Connect(function(plr, price, upgradeAmount, name, mesh)
		
	if statsModule.EvaluateEnergy(plr, price) == true then
		
		statsModule.UpgradeEnergy(plr, upgradeAmount)
		statsModule.SubtractEnergy(plr, statsModule.StatRequired)
		print(statsModule.ReadEnergy(plr))
		
		--[[Generate Inventory Item]]--
		local newInventoryItem = Instance.new("StringValue")
		newInventoryItem.Parent = plr.Inventory
		newInventoryItem.Name = name
		
		local isEquipped = Instance.new("BoolValue")
		isEquipped.Parent = newInventoryItem
		isEquipped.Name = "Equipped"
		isEquipped.Value = true
		
		print(newInventoryItem.Name)
		
		inventoryModule.AddItem(plr, name)
		

Thank you for the help! I can provide other information if needed.

1 Like

You’re assigning the Name property to an object. I assume you meant to do: itemAdded.Name = item.Name

EDIT: What I meant to say was you’re assigning the Name property to a table. What did you want the name to be?

I think that this is not correctly used so instead of calling ‘item’ varaible you should easily use:

local itemAdded = Instance.new("StringValue") 
itemAdded.Parent = inventory
itemAdded.Name = tostring(v[i]) -- Get in pairs table, selected index + value with called tostring func

I edited it to show you the use of name.

Show the script that fires the event. I need to see what arguments you are passing to the event

The name is working, I printed it out and it worked. I have a table that has all the items the user purchases. I’m not sure how to save it now.

function InventorySavingHandler.AddItem(player, item)
	
	local InventoryStore = DataStore2("inventory", player)
	print("Working with ", item)

	table.insert(invTable, item)
	defaultValue = invTable
	print(invTable[1], "Third")
	
	
end

This works and builds a table with the proper naming.

You basically have to serialize the data, the use Set and Save to save it

Here’s a link to the API API - DataStore2
Here’s a link on how to serialize it Serialization - DataStore2

To clarify, ds2 automatically saves and loads though, right?

Also, I don’t think serialization is too much of the issue. I have measures in place already that prevent a user from buying multiple of an item.

Yes, Set sets the value to be saved, Save saves it to the datastore, and Get obtains the serialized data

So would it still need serialization? This gets table, but I can’t loop through the datastore for some reason. It says v is a table, not string.

local InventorySavingHandler = {}

local DataStore2 = require(script.Parent.DataStore2)

local defaultValue = {}
local invTable = {}

game.Players.PlayerAdded:Connect(function(plr)
	
	DataStore2.Combine("DATA", "inventory")
	
	local inventoryDataStore = DataStore2("inventory", plr)
	print("The store is ", inventoryDataStore)
	
	
	inventoryDataStore:SetBackup(5)
	local inventory = plr:WaitForChild("Inventory")
	local inventoryTable = inventory:GetChildren()
	
	local function inventoryUpdate(updateValue)
		
		inventory.Value = inventoryTable:Get(updateValue)

	end
	
	for i, v in pairs(inventoryDataStore) do
		
		local itemAdd = Instance.new("IntValue")
		itemAdd.Name = v
		print(v .. "yeah")
		itemAdd.Parent = inventory
		
		local equipped = Instance.new("BoolValue")
		equipped.Name = "Equipped"
		equipped.Value = false
		
	end
	
	inventoryUpdate(defaultValue)
	inventoryDataStore:OnUpdate(inventoryUpdate)
	
end)

function InventorySavingHandler.AddItem(player, item)
	
	local InventoryStore = DataStore2("inventory", player)
	print("Working with ", item)

	table.insert(invTable, item)
	defaultValue = invTable
	print(invTable[1], "Third")
	table.insert(InventoryStore, item)
	print(InventoryStore[1] .. "is the value")
	
	InventoryStore:Set(invTable)
	
end

return InventorySavingHandler

Well, if it doesn’t have any actual instances like Parts, StringValues and etc., serialization is not necessary

The table is of strings, which are then set to the name of values I instantiate

Then it doesn’t have to be serialized - you just have to use Set and then Save to save it

What am i saving exactly? The table?

Did you ever find an answer? I’m having this exact same issue. My inventory does not save, using :Set() and :Save() on tables does not seem to work. Despite it returning all the player’s tools when it prints, I still get an error message saying “Attempt to call a nil value”