I am currently working on an inventory system but ran into a roadblock.
For some reason, the stacking system isn’t working. I couldn’t find any reasons why.
Script:
local InfoTable = {
['Wood'] = {
Name = 'Wood';
ItemDescription = 'Quack';
ImageId = 0000000000;
Amount = 0;
StackLimit = 2;
};
}
local inventories = {
['Exavism'] = {
[1] = {};
[2] = {};
[3] = {};
[4] = {};
[5] = {};
[6] = {};
[7] = {};
[8] = {};
[9] = {};
};
}
function AddItem(Item) -- Function for adding items
if not InfoTable[Item.Name] then return end -- Checks if the item exists
local Inventory = inventories.Exavism
local FoundSlot = false -- Needed when checking if the loop below finds a slot to put the item in
for SlotOrder = 1, 9 do -- Checks if there are any items that are under the stack limit
if Inventory[SlotOrder][Item.Name] and Inventory[SlotOrder][Item.Name].Amount < InfoTable[Item.Name].StackLimit and not FoundSlot then
FoundSlot = true
Inventory[SlotOrder][Item.Name].Amount = Inventory[SlotOrder][Item.Name].Amount + 1
break -- Breaks loop
end
end
if not FoundSlot then -- Only fires when the loop above is completed. Checks if the loop above has already found an available slot.
for SlotOrder = 1, 9 do -- Checks if there are any items that are under the stack limit
if Inventory[SlotOrder][Item.Name] == nil then -- Checks if there isn't an item inside the slot's table
Inventory[SlotOrder][Item.Name] = InfoTable[Item.Name] -- Creates an item inside the table
Inventory[SlotOrder][Item.Name].Amount = 1
break -- Breaks loop
end
end
end
end
The function can simply be fired using this:
for Count = 1, 5 do
AddItem(InfoTable.Wood)
end
print(inventories.Exavism)
In theory, since the item’s stack limit is 2, and the ‘AddItem()’ function is fired 5 times, it should use 3 slots. It works as shown here;
But for some reason, this part:
Inventory[SlotOrder][Item.Name].Amount = 1
is setting every single ‘Wood’ item on the inventory’s amount to 1.
How can I fix this?