• Help with Table.Random?
    3 replies, posted
Hello, so I am trying to ask how to get the output of table.random and use it? Here is my code: [CODE]hook.Add( "PlayerSay", "Class12", function( ply, text, public ) text = string.lower( text ) if ( text == "/house" ) and ply:IsUserGroup("user") then house = { "Hufflepuff", "Ravenclaw", "Slytherin", "Gryffindor" } ply:ChatPrint(house[math.random( 1, #house )]) if ---- this is where i need help, what do i type here to say if (random output) = "Hufflepuff" then *insert code*? end end end )[/CODE] Any help would be much appreciated! And in case you are wondering, this is a part of a hogwarts rp script i'm making.
local val, key = table.Random(tbl) print(key, "=", val)
Assign the random value a variable instead, i.e. [code] local randName = house[math.random(#house)] ply:ChatPrint(randName) if randName == "Hufflepuff" then -- do shit end [/code] Also use local variables instead of globals. [QUOTE=txike;52119759]local val, key = table.Random(tbl) print(key, "=", val)[/QUOTE] You shouldn't use this function for sequential tables, even the wiki page states this.
[QUOTE=bigdogmat;52119875]Assign the random value a variable instead, i.e. [code] local randName = house[math.random(#house)] ply:ChatPrint(randName) if randName == "Hufflepuff" then -- do shit end [/code] Also use local variables instead of globals. You shouldn't use this function for sequential tables, even the wiki page states this.[/QUOTE] Thanks for the help! I got it working. The code I used was [CODE]if (table.Random( house ) == "Hufflepuff") then ply:ChatPrint("You have been selected for the Hufflepuff house!") RunConsoleCommand( "Addwhitelist", "Console", ply:SteamID64(), ply:Nick(), "Hufflepuff" ) RunConsoleCommand( "ulx", "adduser", ply:Nick(), "member" ) end[/CODE]
Sorry, you need to Log In to post a reply to this thread.