I have two bricks in workspace and when I click one of them they are supposed to be inserted into a table, and If the player already has the item it will just increase their quantity.
It works perfectly if they click the item 1 first and then the second but if I click the second item first it will error. My guess is that it’s not iterating through the entire table.
ReplicatedStorage.InventorySystem:26: attempt to index field '?' (a nil value)
the script
local InventorySystem = {}
InventorySystem.Items = {
[1] = {Name = "Axe", Quantity = 1},
[2] = {Name = "Pickaxe", Quantity = 1},
}
InventorySystem.Inventories = {}
InventorySystem.CreateInventory = function(player)
InventorySystem.Inventories[player.Name] = {}
print("//Created a table for " .. player.Name)
end
InventorySystem.AddItemInventory = function(player, Item)
local id
for i,v in ipairs(InventorySystem.Items) do
if Item.Name == v.Name then
id = i
if InventorySystem.Inventories[player.Name][id] then
InventorySystem.Inventories[player.Name][id].Quantity = InventorySystem.Inventories[player.Name][id].Quantity + 1
print(InventorySystem.Inventories[player.Name][id].Name .. InventorySystem.Inventories[player.Name][id].Quantity)
else
table.insert(InventorySystem.Inventories[player.Name], InventorySystem.Items[id])
print("//Added " .. InventorySystem.Inventories[player.Name][id].Name .. " to " .. player.Name .. "inventory")
end
end
end
end
return InventorySystem