How do I insert something in sub tables?

How would I table.insert something for example, in weapons > guns
This is my table:

local Inventory = {
    weapons = {
        guns = {},
        swords = {},
    },

	items = {},
}

This is my attempt:

table.Insert(Inventory) -- inventory > weapons > guns ???

If you wanted to insert something Guns for example

table.insert(Inventory["weapons"]["guns"], yourvalue)
1 Like

I tried it but it says:
attempt to index nil with 'guns'

This is my script:

local function giveItem(profile, name, rarity, status)
	table.insert(profile["Inventory"]["guns"], {name, rarity, status})
end

How does your profile table look like?

local profile = {
	Inventory = {
		guns = {},
		swords = {},
	},
}

What do you get when you print the table in the function?

I think you want to use “profile” as the table name, so it should look like this:

local function giveItem(profile, name, rarity, status)
	table.insert(profile["weapons"]["guns"], {name, rarity, status})
end

-- Calling Function
giveItem("Inventory", "GunName", "GunRarity", "GunStatus")
1 Like