Set key to specified string

Hey, I’m creating a shop with upgrades.
I got upgrades work out like this:

[lua]
GMShop = {}
function AddUpgrade(Name, Description, Model, Upgrades)
local NewItem = {description = Description, model = Model, upgrades = Upgrades}
NewItem.name = Name

// WE HAVE TO MAKE THE KEYS STRINGS WITH UPGRADE NAME, ELSE IT GOES FUCK ONCE UPGRADES ARE UPDATED!

table.insert(GMShop, NewItem)
util.PrecacheModel(NewItem.model)

end

UPGRADE_HEALTH = AddUpgrade(“Health”, “Increase your max health, to survive longer.”, “models/healthvial.mdl”, {
{price = 100, value = 10, donator = false},
{price = 200, value = 20, donator = false},
{price = 300, value = 30, donator = false},
{price = 400, value = 40, donator = false},
{price = 500, value = 50, donator = false},
{price = 600, value = 70, donator = true},
{price = 800, value = 100, donator = true}
})
[/lua]

The question is, each upgrade now get’s key value 1-2-3-4 etc. Is it possible to somehow make it a specified string? like “healthupgrade”. I know how to get it inside the function, but not sure how to insert it to inside the table to make that work.

The reason for why I’m doing this, is because if I sometime change/remove/add upgrades from the shop, the ID’s will change, and the people’s values that indicate what they’ve bought, might get messed up.

You can use strings as keys for tables.

[lua]GMShop[name] = NewItem[/lua]

Hmm, something like this doesn’t quite work, just errors out.
I understand how to do it, but not how to do it through table.insert :confused:

[lua]
table.insert(GMShop[Name], NewItem)
[/lua]

You don’t use table.insert, you literally do it how Walrus said to.

Oh of course. I thought that way it would override the table.
Weak moment.

Thanks!
-solved

Here’s what I’m talking about:



shop = {}

local function Add(name, desc, mdl, upgrades)
	-- make upgrades non-donator by default
	for k, v in pairs( upgrades ) do
		v.donator = v.donator or false
	end
	
	shop[name] = {
		desc = desc,
		model = model,
		upgrades = upgrades
	}
	
	util.PrecacheModel(mdl)
end


local healthUpgrades = {}

Add("Health", "Increase your max health to survive longer.", "models/healthvial.mdl", {
	{price = 100, value = 10},
	{price = 200, value = 20},
	{price = 300, value = 30},
	{price = 400, value = 40},
	{price = 500, value = 50},
	{price = 600, value = 70, donator = true},
	{price = 800, value = 100, donator = true}
})