• Make methods to happen for every player
    5 replies, posted
I have found this: [url]http://wiki.garrysmod.com/page/player/GetAll[/url] but im new with lua I want to make something to print for every players, for example player:PrintMessage to happen to every player online.
for k,v in pairs( player.GetAll() ) do v:PrintMessage( HUD_PRINTTALK, "hello" ) end
[QUOTE=Invule;48237125]for k,v in pairs( player.GetAll() ) do v:PrintMessage( HUD_PRINTTALK, "hello" ) end[/QUOTE] Can you explain the for loop for me? I have never understood the k,v in pairs stuff
for key, value in pairs( player.GetAll() ) print( key, value:Nick() ) end output: 1 Invule. Where the KEY is 1 and the VALUE is Invule edit: You can also read about it here. [url]http://wiki.garrysmod.com/page/Global/pairs[/url]
k it's the player index number, the first player to join normally will be 1, and the last player will be like 4 or 5 (In the case there are 4 or 5 players) and v it's the player itself it's for key, values, key it's the index of the array: [lua] local tbl = {apple="ok",banana="rock"} local ntbl = {"ok,"rock"} for k,v in pairs(tbl) do MsgN(k.." "..v) end for k,v in pairs(ntbl) do MsgN(k.." "..v) end //Will print: //apple ok //banana rock //1 ok //2 rock [/lua]
[QUOTE=gonzalolog;48237159]k it's the player index number, the first player to join normally will be 1, and the last player will be like 4 or 5 (In the case there are 4 or 5 players) and v it's the player itself it's for key, values, key it's the index of the array: [lua] local tbl = {apple="ok",banana="rock"} local ntbl = {"ok,"rock"} for k,v in pairs(tbl) do MsgN(k.." "..v) end for k,v in pairs(ntbl) do MsgN(k.." "..v) end //Will print: //apple ok //banana rock //1 ok //2 rock [/lua][/QUOTE] Thanks, you explained it very good :)
Sorry, you need to Log In to post a reply to this thread.