How to search for one of multiple values assigned to one key
2 replies, posted
So I'm making a pawn shop addon for my server. I have a table and inside it has a key, then two value. 1) the entity you can sell. 2) how much its worth. Then I crossreference items near the pawn shop guy to the ones that can be sold and I have no problem finding out which ones are sellable. But I cannot for the life of me figure out how to grab the price from that key.
Here, Ill just speak LUA.
This is my table.
allowedentities["ecl_cocaine"] = {
[1] = {
name = "pawnshop_baby",
price = 500
},
[2] = {
name = "pawnshop_beer",
price = 500
},
[3] = {
name = "pawnshop_bike",
price = 500
},
[4] = {
name = "pawnshop_briefcase",
price = 500
},
[5] = {
name = "pawnshop_jar",
price = 500
},
[6] = {
name = "pawnshop_keyboard",
price = 500
},
[7] = {
name = "pawnshop_pan",
price = 500
},
[8] = {
name = "pawnshop_server",
price = 500
},
[9] = {
name = "pawnshop_shoe",
price = 500
},
[10] = {
name = "pawnshop_wrench",
price = 500
}
}
I somehow need to figure out how to grab that price value from the key containing the name to pay the player. Maybe make the key the entity and the price the value? Im lost. Any help is appreciated. thank you
You could also do something like the example below to avoid unneeded loops.
allowedentities["ecl_cocaine"] = {
["pawnshop_baby"] = {
price = 500
},
["pawnshop_beer"] = {
price = 500
},
["pawnshop_bike"] = {
price = 500
}
}
local function Sell(ply, item)
if not allowedentities["ecl_cocaine"][item] then return end
local item = allowedentities["ecl_cocaine"][item]
ply:TakeMoney(item.price)
ply:Give(item)
end
@dubb thank you a bunch. it helped out a ton. my solution was garbo. My table skills are lacking heavily, so I appreciate it.
Sorry, you need to Log In to post a reply to this thread.