Looping through a players inventory table

I am trying to make an inventory system.
I currently have a table used to store a players data like this in a module script

PlayersData= {
	["Gold"] = 100,
	["Level"] = 1,
	["Inventory"] = {
		["Weapons"] = {
			[1] = {
			    ["Weapon"] = "Sword",
                -- other information
			}
		},
		["Armour"] = {
		
		},
		["Helmet"] = {
		
		},
		["Spells"] = {
			
		}
	},
	["Equiped"] = {
		["Equiped_Weapon"] = nil,
		["Equiped_Helmet"] = nil,
		["Equiped_Armour"] = nil,
		["Spell_1"] = nil,
		["Spell_2"] = nil
	}
	
}

I am having trouble looping through the players inventory table. I want to be able to get all the items from the players inventory and place them into a grid layout.

Would I use a for loop to get the tables inside PlayersData[“Inventory”] then if the table is not nil use a for loop to get the items inside?

1 Like
for i,v in pairs(PlayerData["Inventory"]) do
    if v then
       for a,b in pairs(v) do
             if a == "Weapons" then
                for c,d in pairs(b) do
                   -- im going to create my own template for this just so you can see how it works
                   local temp = game:GetService("ReplicatedStorage").Template:Clone()
                   temp.TypeOfItem.Text = c["Weapon"]
                   -- add in all the other info
                   temp.Parent = script.Parent.Holder
                end
            elseif a == "Armour" then
                --code
            end
        end
    end
end

i havent tested this so please tell me if it doesn’t work

5 Likes