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.
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
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.
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
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”