Trying to get started with OOP and decided to make an Inventory object
Here is my ModuleScript called "Inventory"
local Inventory = {} Inventory.__index = Inventory function Inventory.New(Player) return setmetatable({ Player = Player; Backpack = { ["Coins"] = {Name = "Coins", Quantity = 0, Description = "Lovely money!"} }; }, Inventory) end function Inventory:AddItem(Item, Quantity) if self.Backpack[Item] and Quantity >= 0 then -- Check if the Item is an actual Item available in the backpack and -- were adding a positive number self.Backpack[Item].Quantity = self.Backpack[Item].Quantity + Quantity return self.Backpack[Item].Quantity end return false end function Inventory:GetQuantity(Item) if self.Backpack[Item] then -- Check if the Item is an actual Item available in the backpack return self.Backpack[Item].Quantity end return false end return Inventory
My issue is that when I run Inventory:GetQuantity("Coins") I get the error:
ServerScriptService.Inventory:24: attempt to index field 'Backpack' (a nil value)
How come that doesn't work but when I call Inventory:AddItem("Coins") it works? The issue might not be because of OOP and I'm just not seeing something.
Found my mistake.
In the server Script (Not the module) I was doing
Inventory:GetQuantity("Coins")
not the correct way:
NewInventory:GetQuantity("Coins")
Feel kinda stupid, but I'm glad I "fixed" it