Inventory system concerns

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

add a new table for all the players inventory

Inventory.PlayersInventory[Player1] = {}

example ^

then you can just require the module and get it then call a function to remove the player from the table

Surely there’s a way to get the inventory I made though instead of making another one

yea theres lots of ways

one saving it inside the script then make the data script call it and grab it or do it through the module table or other ways

also ur not making another one, your just storing it for global use case

1 Like

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.

You will be able to retrieve it by calling the function, if I’m not mistaken.

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.

I’ll give that a try thanks for your advice

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

Weird. Are the items printed, or is it just the owner not being printed?

1 Like

This is what it prints basically everything inside of the module table:

Click the arrow (image). That should be the table.

Looked like it would be ngl haha but I don’t think so

Is anything in here ran? Try adding a print(‘test’) or something.

Nah I didn’t run anything in that yet, I’m just tryna get the owner variable from the table.

Are you even running the .new() function?? That’s the only thing I can think of.

1 Like
local InventorySystem = require(script.Parent.InventoryService)
game.Players.PlayerAdded:Connect(function(Player)
	local Inventory = InventorySystem.new(nil, Player)
end)
game.Players.PlayerRemoving:Connect(function(Player)
	local GetInventory = InventorySystem:GetPlayerInventory(Player.Name)
end)

Yeah it’s inside of this script

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?

1 Like

Well I have an idea, but I don’t think it’s the best one. I’ll give it a try real quick and you can tell me what you think.