-1

So I have a table that looks something like this:

local weapons = {[46]= "Megapositron", [173]= "Sunflare", [702]= "raven"}

I also have a variable:

weaponid 

The variable weaponid will contain one of the keys from weapons (46,173,702)

What I can't figure out is how to check if weaponid matches one of those keys and upon weapon id matching a key I need it to assign the keys value to another variable named weaponname.

So suppose weaponid = 173 then weaponname = 'Sunflare'

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
moretimetocry
  • 73
  • 1
  • 1
  • 4

2 Answers2

2
local weaponname = weapons[weaponid]

weaponname will be nil if the id doesn't match any of the keys.

John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
0
weaponname = weapons[weaponid] or weaponname

This way, the old weapon name will be kept if there is no weapon associated to the new id.

Jan Berktold
  • 976
  • 1
  • 11
  • 33