Hello, I am trying to Insert a value into a table that is inside of another table. I use a for loop to loop through the other tables and see if an object inside of the table and if it isn’t then add it to the table. The issue that im getting is the if statement to check if the item is inside of the table and I get an error saying attempt to index nil with 'object'. This script is a local script.
local function AddItemToTable(tbl)
local plr = Players.LocalPlayer
local T1 = Inventory.T1[plr.Name]
local T2 = Inventory.T2[plr.Name]
local T3 = Inventory.T3[plr.Name]
local T4 = Inventory.T4[plr.Name]
for i,v in pairs(tbl) do
local itemType = ItemModule[i]["Type"]
--Error happening here
if i ~= T1[i] or T2[i] or T3[i] or T4[i] then
Inventory[itemType][plr.Name][i] = Inventory[itemType][plr.Name][i] + 1
elseif i == T1[i] or T2[i] or T3[i] or T4[i] then
Inventory[itemType][plr.Name][i] = 1
end
end
end
i prints the items name and v prints the amount. can someone please help? This is really confusing me.
This error means that T1, T2, T3 or T4 is nil, that is, there is no table for that player. To solve it you can create a table if the player does not have one
local function AddItemToTable(tbl)
local plr = Players.LocalPlayer
if not Inventory.T1[plr.Name] then Inventory.T1[plr.Name] = {} end
if not Inventory.T2[plr.Name] then Inventory.T2[plr.Name] = {} end
if not Inventory.T3[plr.Name] then Inventory.T3[plr.Name] = {} end
if not Inventory.T4[plr.Name] then Inventory.T4[plr.Name] = {} end
local T1 = Inventory.T1[plr.Name]
local T2 = Inventory.T2[plr.Name]
local T3 = Inventory.T3[plr.Name]
local T4 = Inventory.T4[plr.Name]
for i,v in pairs(tbl) do
local itemType = ItemModule[i]["Type"]
if T1[i] or T2[i] or T3[i] or T4[i] then
Inventory[itemType][plr.Name][i] = Inventory[itemType][plr.Name][i] + 1
else
Inventory[itemType][plr.Name][i] = 1
end
end
end
if i ~= T1[i] or i ~= T2[i] or i ~= T3[i] or i ~= T4[i] then --here
Inventory[itemType][plr.Name][i] = Inventory[itemType][plr.Name][i] + 1
elseif i == T1[i] or i == T2[i] or i == T3[i] or i == T4[i] then --and here
Inventory[itemType][plr.Name][i] = 1
end