• If part of a table exists?
    5 replies, posted
I need to have an if statement for if part of a table exists For example, I have: [CODE]local allowedToPickup = { weapon_mad_deagle = "Deagle" }[/CODE] How would I check to see if 'weapon_mad_deagle' exists?
[QUOTE=Sethxi;40225084]I need to have an if statement for if part of a table exists For example, I have: [CODE]local allowedToPickup = { weapon_mad_deagle = "Deagle" }[/CODE] How would I check to see if 'weapon_mad_deagle' exists?[/QUOTE] You could change the weapon_mad_deagle to a boolean, then check if it is true or false
You don't need a boolean, if it isn't nil then it should work. Anyways: [lua] if (allowedToPickup.weapon_mad_deagle) then -- exists end [/lua]
[lua]if allowedToPickup["weapon_mad_deagle"] then -- do something end[/lua] In this example, you're accessing 'weapon_mad_deagle' by its key index in the table. If it didn't exist, the statement would return nil and the condition would be false.
[lua] local allowedToPickup = { Deagle = "weapon_mad_deagle" } if table.HasValue( allowedToPickup, "weapon_mad_deagle" ) then --suck a lolipop end[/lua]
[QUOTE=Blue Kirby;40226601][lua] local allowedToPickup = { Deagle = "weapon_mad_deagle " } if table.HasValue( allowedToPickup, "weapon_mad_deagle" ) then --suck a lolipop end[/lua][/QUOTE] He's trying to check if the key existed, not the value.
Sorry, you need to Log In to post a reply to this thread.