yea, I referenced it and then Im calling it and im sending the data to the players name.
When you do Inventory.T1[plr.Name]
, its nil. You have to add in a table to that key: Inventory.T1[plr.Name] = {}
. Now you can index it.
I added This:
local T1 = Inventory.T1[plr.Name]
local T2 = Inventory.T2[plr.Name]
local T3 = Inventory.T3[plr.Name]
local T4 = Inventory.T4[plr.Name]
T1 = {}
T2 = {}
T3 = {}
T4 = {}
but still nothing
You can’t do that. You’re just replacing an existing variable with a new table. You’re not adding a table to the key.
how do you think I should reference it then
Inventory.T1[plr.Name] = {}
local T1 = Inventory.T1[plr.Name]
Now I got a new error saying
attempt to perform arithmetic (add) on nil and number
Can you see your new script? -
local Inventory = {
T1 = {};
T2 = {};
T3 = {};
T4 = {};
}
Inventory.T1[plr.Name] = {}
Inventory.T2[plr.Name] = {}
Inventory.T3[plr.Name] = {}
Inventory.T4[plr.Name] = {}
local T1 = Inventory.T1[plr.Name]
local T2 = Inventory.T2[plr.Name]
local T3 = Inventory.T3[plr.Name]
local T4 = Inventory.T4[plr.Name]
local function AddItemToTable(tbl)
for i,v in pairs(tbl) do
local itemType = ItemModule[i]["Type"]
if i ~= T1[i] or i ~= T2[i] or i ~= T3[i] or i ~= T4[i] then
Inventory[itemType][plr.Name][i] = Inventory[itemType][plr.Name][i] + 1 --error happening here
elseif i == T1[i] or i == T2[i] or i == T3[i] or i == T4[i] then
Inventory[itemType][plr.Name][i] = 1 --and probably here
end
end
end
Inventory[itemType][plr.Name][i]
This is nil. Remember what you did. You just added a new table to that key. The table is still empty, it cant add 1 to nil.
so how will I add the value then. because itemType is the exact same name as T1 or T2 etc.
what value that you will add? seems like you want to insert key with a value
Im using table.insert but whenever I pick up a different item the item that was used before gets multiplied.
code? but what type of value it is i.e Number, String, Boolean
local function AddItemToTable(tbl)
for i,v in pairs(tbl) do
local itemType = ItemModule[i]["Type"]
if i ~= T1[i] or i ~= T2[i] or i ~= T3[i] or i ~= T4[i] then
table.insert(Inventory[itemType], i)
elseif i == T1[i] or i == T2[i] or i == T3[i] or i == T4[i] then
table.insert(Inventory[itemType], i)
end
end
end
The value is a string but is there a way I can make it insert the object once and then edit the amount. If not then I can just cycle through and get all the values.
object what? table.insert() is bad to be honest
most of the information you need is above. The object is the item that I pickup. When I pick up a different item that the first item still gets items added to it.
you can get the current index by printing it and check if i is not 1
debugging
How do you clear a for loop because when I pick up item1 it continues to count up but when I pick up item2 item1 also goes up as well as item2
to break a for loop simply type break in the end of it
try to use ipairs also