Hello!
I’m getting an error at line 25
“attempt to index nil with Instance”
I’ve tried a lot of methods to fix this but they ain’t worked out very well.
Here’s the full code:
local config = require(script.Parent.config)
local Inventory = {}
Inventory.__index = Inventory
function Inventory.new(plr: Player)
local self = setmetatable({}, Inventory)
self.InventorysPerPlayer = {}
self.Inventory = {}
self.SPACE_LIMIT = config.SPACE_LIMIT
table.insert(self.InventorysPerPlayer, plr)
self.InventorysPerPlayer[plr] = self.Inventory
self.InventorysPerPlayer[plr][#self.Inventory+1] = config.items[math.random(1,#config.items)] -- gets an random item and put it in inventory
return self
end
function Inventory:Init()
print("Inventory was initialized")
end
function Inventory:GetInventory(plr: Player)
return self.InventorysPerPlayer[plr] or {} -- the problem
end
function Inventory:GetItems(plr: Player)
local inventory = self:GetInventory(plr)
local items = {}
local i = 0
for _, item in ipairs(inventory) do
items[i+1] = item
end
return items
end
function Inventory:AddItem(plr: Player, item: any)
local inventory = self.GetInventory(plr)
if (#inventory == self.SPACE_LIMIT) or (#inventory >= self.SPACE_LIMIT) then
warn(plr.Name .. "'s inventory is full!")
return
end
inventory[#inventory+1] = item
print(item .. " has been added to " .. plr.Name .. "'s inventory!")
end
function Inventory:RemoveItem(plr: Player, item)
local inventory = self.InventoryPerPlayer[plr]
if inventory[item] == nil then return end
inventory[plr] = nil
print(item .. " has been removed from " .. plr.Name .. "'s inventory!")
end
function Inventory:Destroy(plr: Player)
self.InventorysPerPlayer[plr] = nil
end
return Inventory
Anything will be appreciated!