Garry's Mod Wiki

player.Iterator

Description

Returns a Stateless Iterator for all players on the server. Intended for use in Generic For-Loops.
See ents.Iterator for a similar function for all entities.

Internally, this function uses cached values that are stored in Lua, as opposed to player.GetAll, which is a C++ function. Because a call operation from Lua to C++ and with a return back to Lua is quite costly, this function will be more efficient than player.GetAll.

Returns

1 function
The Iterator Function from ipairs.
2 sequential table<Player>
Table of all existing Players. This is a cached copy of player.GetAll.
This table is intended to be read-only.

Modifying the return table will affect all subsequent calls to this function until the cache is refreshed, replacing all of your player.GetAll usages may come with unintended side effects because of this.

An example:

-- Bad code. local scan_ents = select( 2, player.Iterator() ) table.Add( scan_ents, ents.FindByClass( "ttt_decoy" ) ) -- Fine code. A copy of the table is being made and used. local scan_ents = table.Copy( select( 2, player.Iterator() ) ) table.Add( scan_ents, ents.FindByClass( "ttt_decoy" ) )
3 number
The starting index for the table of players.
This is always 0 and is returned for the benefit of Generic For-Loops.

Example

Prints out all players to the console.

for _, ply in player.Iterator() do print( ply ) end