I want to retrieve the players inventory when the player leaves so that I can save it. But I can’t do that if I don’t know how to get the player’s inventory from my script.
I’ve been practicing with OOP and decided to use it for this system.
local Inventory = {}
Inventory.__index = Inventory
function Inventory.new(Items, Owner)
local NewInventory = {}
NewInventory.Owner = Owner
setmetatable(NewInventory, Inventory)
if Items then
for ItemName, ItemProperties in pairs(Items) do
NewInventory[ItemName] = {Level = ItemProperties.Level, Exp = ItemProperties.Exp}
end
end
return NewInventory
end
function Inventory:AddItemToInventory(ItemName, ItemProperties)
self[ItemName] = {Level = ItemProperties.Level, Exp = ItemProperties.Exp}
end
function Inventory:GetPlayerInventory(Owner)
-- This part is where I'm confused
for i,v in pairs(Inventory) do
print(i,v) -- All this prints is everything inside of Inventory
-- But how am I supposed to get NewInventory?
end
end
return Inventory
I would personally just store it in a table (like Mewious) said because it’s the most accessible and simplest to do.
local PlayerInventoryTable = {}
local Inventory = {}
Inventory.__index = Inventory
function Inventory.new(Items, Owner)
local NewInventory = {}
NewInventory.Owner = Owner
setmetatable(NewInventory, Inventory)
if Items then
for ItemName, ItemProperties in pairs(Items) do
NewInventory[ItemName] = {Level = ItemProperties.Level, Exp = ItemProperties.Exp}
end
end
PlayerInventoryTable[Owner] = NewInventory
return NewInventory
end
function Inventory:AddItemToInventory(ItemName, ItemProperties)
self[ItemName] = {Level = ItemProperties.Level, Exp = ItemProperties.Exp}
end
function Inventory:GetPlayerInventory(Owner)
-- This part is where I'm confused
for i,v in pairs(Inventory) do
print(i,v) -- All this prints is everything inside of Inventory
-- But how am I supposed to get NewInventory?
end
end
return Inventory
We can now get the inventory of a player by doing this:
function Inventory:GetPlayerInventory(Owner)
local PlayerInventory = PlayerInventoryTable[Owner]
if PlayerInventory then
for _,v in PlayerInventory do
print(v)
end
end
end
So I assume that since the table is created locally inside of Inventory.new that means I won’t be able to retrieve it unless I add a copy of the table into PlayerInventoryTable which is what you guys came up with.
Your OOP script is really strangely set-up. Try changing NewInventory to this:
-- Old:
local NewInventory = {}
-- New:
local self = setmetatable({}, Inventory)
-- Replace all occurences of NewInventory with self
-- Also remove setmetatable(NewInventory, Inventory)
-- Now, you can get the items by looping through self.
I should probably explain what self does as you do not seem to use/acknowledge it here. self is basically a global variable that you initialize with the code above in a .new() function.
self is then used from colon functions (eg. part:Destroy()) to get variables set from other colon functions or the .new() function. Hope this helps.
Nah it’s the same issue for me it prints the table but not the contents of the table like the owner.
local Inventory = {}
Inventory.__index = Inventory
function Inventory.new(Items, Owner)
local self = setmetatable({}, Inventory)
self.Owner = Owner
if Items then
for ItemName, ItemProperties in pairs(Items) do
self[ItemName] = {Level = ItemProperties.Level, Exp = ItemProperties.Exp}
end
end
return self
end
function Inventory:AddItemToInventory(ItemName, ItemProperties)
self[ItemName] = {Level = ItemProperties.Level, Exp = ItemProperties.Exp}
end
function Inventory:GetPlayerInventory(Owner)
for i,v in pairs(self) do
print(i,v)
end
end
return Inventory
Aha! You aren’t using the Owner variable at all in GetPlayerInventory. This makes us… loop back to step 1 and figure out how you would even do this in lua!
Maybe create a table inside the module with the names, add the owner’s name to that table in .new, and when GetPlayerInventory is called, look for that name in the table with string.find?