Inserting another value into a table

I am trying to add a third value to the “chosenitem” table called “Rarity”, which will hold the value of the rarity variable.

I’ve tried using table.insert but it only returned the rarity by itself.

image

function itemFromRarity(rarity)
	local availabeItems = packInformationModule.Rewards[rarity]
	local chosenItem = availabeItems[math.random(1,#availabeItems)]
	
	print(chosenItem)
	return chosenItem
	
end
1 Like

For example:

My goal is to have it return as

[“Amount”] = 5,
[“Name”] = “Sap”,
[“Rarity”] = rarity value

1 Like

Does

tbl['Rarity'] = 'Rare'

not work?

image

There is no rarity value in the table. I actually just have it get the values of the item in this module. I’m trying to add it in.

since the rarity is already used to find the item, it would be a shame to just add an extra “Rarity” value into the table.

Okay, if I’m understanding it correctly, can’t you just add the rarity when you get the item? Or you can run a for loop when the module is required and give each item a “Rarity” value corresponding to the table they’re in.

No the module is server-sided. I’m wondering how to just add another value to the table with the script I have.

You’re working with a dictionary.

table.insert(array, value)

is for arrays. A dictionary is a table with specific keys, an array is numbered. This also means ipairs doesn’t work for dictionaries.

yourTable["yourKey"]=yourValue

This’ll add your value to the dictionary, at which point you can get the value by indexing the key.

Got it. Thank you so much for your help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.