I’ve been working on creating an inventory system using Lua tables. Initially, I had implemented the system using string values inside a folder, but I wanted to explore the possibility of using tables for better organization. I would greatly appreciate your expertise in reviewing my code and suggesting potential improvements.
I’ve developed an inventory system where players can own various items. However, as a beginner and not having much experience with tables, I have concerns about the efficiency and overall quality of my code
possibleItems={
melonseed={type="seed",maxStack=100},
melon={type="crop",maxStack=20}
}
local InventoryModule = {}
--//Otehr Functions//--
local function numerotateItemNames(itemTable, baseName)
local highestIndex = 0
-- Find the highest index in the table
for itemName, _ in pairs(itemTable) do
local index = tonumber(itemName:match(baseName .. "(%d+)"))
if index and index > highestIndex then
highestIndex = index
end
end
-- Increment the highest index to get the next available index
local nextIndex = highestIndex + 1
return baseName .. nextIndex
end
local function splitStacks(player) --Splits the stacks that exceed the maxStackLimit
local inventory = InventoryModule[player.Name].items
for itemName, itemData in pairs(inventory) do
if itemData.quantity > possibleItems[itemData.name].maxStack then
while itemData.quantity > possibleItems[itemData.name].maxStack do
itemData.quantity = itemData.quantity - possibleItems[itemData.name].maxStack
local newname = numerotateItemNames(inventory, itemData.name)
print(newname)
-- Create a new item entry in the inventory
inventory[newname] = {
name = itemData.name,
quantity = possibleItems[itemData.name].maxStack
}
end
end
end
end
-----------------------
--///MAIN FUNCTIONS///--
function InventoryModule:AddInventory(player) --//Function that adds an inventory to a player
local inventory={
items={
}
}
if not InventoryModule[player.Name] then
InventoryModule[player.Name]=inventory
else
warn ("inventory already exists")
end
end
function InventoryModule:AddItem(player,item,quantity) --adds an item to the players inventory e.g(melonseed)
local inventory=InventoryModule[player.Name].items
if InventoryModule[player.Name] then
for itemName,itemData in pairs(inventory) do --if script finds item with the same baseName then adds the quantity to it
if itemData.name==item then
itemData.quantity+=quantity
splitStacks(player)
return
end
end
--//Else creates a new item with the baseName and quantity
inventory[item]={name=item,quantity=quantity}
splitStacks(player)
else
warn("inventory does not exist !")
end
end
function InventoryModule:RemoveItem(player,item,quantity) --removes a specific item from the players inventory e.g(melonseed1)
local inventory=InventoryModule[player.Name].items
if InventoryModule[player.Name] and inventory[item] then
inventory[item].quantity-=quantity
if inventory[item].quantity<0 or inventory[item].quantity==0 then
inventory[item].quantity=0
inventory[item]=nil
end
else
warn("inventory or item does not exist !")
end
end
------------------------
--//DebbugingFunctions//--
function InventoryModule:DebugPrint(plr)
local inventory = InventoryModule[plr.Name]
if inventory then
for itemName, itemData in pairs(inventory.items) do
print(itemName, itemData.quantity)
end
end
end
return InventoryModule
--------------------------