How can I use a variable of a table that's in a table?
2 replies, posted
local bmInv = {
moneyPalette = {
name = "Illegal Money Palette",
model = "models/props/cs_assault/MoneyPallet.mdl",
buyPrice = 10000,
sellPrice = 25000,
},
illegalDrugs = {
name = "Drugs",
model = "models/props_junk/metal_paintcan001a.mdl",
buyPrice = 1000,
sellPrice = 2000,
},
}
How would I get the name of moneyPalette when running a for loop on bmInv?
I would expect
for k,v in pairs(bmInv) do print(bmInv.k.name)
end
to print
Illegal Money Palette
Drugs
to console, but instead it gives me
'attempt to index field 'k' (a nil value)'
Im kinda stumped here, so any help is appreciated.
k and v will be your Key and Value, so the keys will be the name of the table (moneyPalette, illegalDrugs etc), and the Value will be the table itself, you will then need to access the other keys in your table, so you will need something like this
for k, v in pairs(bmInv) do
print(v.name)
end
Oh, okay, so I was just being stupid. Thanks for the help, and sorry I wasted your time XD
Sorry, you need to Log In to post a reply to this thread.