• Quick table question
    4 replies, posted
I have a quick question, if I have this table: Settings = Settings or {} [CODE] Settings.GroupTags = { ["superadmin"] = { groupColor = Color(255, 76, 76, 100), groupRank = "OWNER", isStaff = true }, }[/CODE] How do I use table.HasValue to see if "superadmin" exists within Settings.GroupTags It appears no matter how I try to flip my code around, it skips that if condition and goes to else as if it cannot see "superadmin" in the table. [CODE] if table.HasValue( Settings.GroupTags, ply:GetUserGroup() ) then draw.SimpleText( Settings.GroupTags[ply:GetUserGroup()].groupRank, "RankName", 72, h / 1.4, Settings.GroupTags[ply:GetUserGroup()].groupColor, 0, 1 ) else draw.SimpleText( "PLAYER", "RankName", 72, h / 1.4, Color(0,255,0,255), 0, 1) end[/CODE] And yes I have ply defined to LocalPlayer() just wanted to keep the code simple because I know it's somewhere just in this area. Thanks in advance
HasValue loops through the table and check for the Values not the Keys if( Settings.GroupTags[ ply:GetUserGroup() ], this returns nil if the key doesn't exist [CODE] local test = { "superadmin", "admin" }; print( table.HasValue( test, "superadmin" ) ) ; // true print( test[ 1 ] ); // superadmin print( test[ "superadmin" ] ); // nil [/CODE]
I see, so what would be the most affective way to set this up so I can check to see if the person's usergroup exists within the table, and then if it does continue to the next line, so I can call the color, etc.
[QUOTE=richardlb;47735151]I see, so what would be the most affective way to set this up so I can check to see if the person's usergroup exists within the table, and then if it does continue to the next line, so I can call the color, etc.[/QUOTE] [CODE] if( Settings.GroupTags[ ply:GetUserGroup() ] ) then local color = Settings.GroupTags[ ply:GetUserGroup() ][ "groupColor" ]; [/CODE] see: a table is table[ key ] = value if you make a table as table = { "here is value 1", 123, 345 }; then table[ 2 ] = 123, if you do: table = {}; table[ 123 ] = 345; then the Key is 123 and holds the Value 345 table[ 1 ][ "groupColor" ] = Color( 255, 255, 255, 255 ); here it's assumed that the value that key 1 holds is another table, which (possibly amongst other things) has a key called groupColor which holds a Color
or wait, re-reading that again (sorry) I should completely drop table.hasvalue all together and use something as to what you posted, a conditional statement just looking for the usergroup within the table. [editline]16th May 2015[/editline] Thanks. I'll try. Hopefully this does the trick. Was assuming hasvalue was the right way to go. [editline]16th May 2015[/editline] Thanks Busan1. That helps a lot. Appreciate your time and helping with this.
Sorry, you need to Log In to post a reply to this thread.