Garry's Mod Wiki

player

The player library is used to get the Lua objects that represent players in-game.

Methods

Player player.CreateNextBot( string botName )
Similar to the serverside command "bot", this function creates a new Player bot with the given name. This bot will not obey to the usual "bot_*" commands, and it's the same bot base used in TF2 and CS:S. The best way to control the behaviour of a Player bot right now is to use the GM:StartCommand hook and modify its input serverside. Despite this Player being fake, it has to be removed from the server by using Player:Kick and NOT Entity:Remove. Also keep in mind that these bots still use player slots, so you won't be able to spawn them in singleplayer!Any Bot created using this method will be considered UnAuthed by Garry's Mod
table player.GetAll()
Gets all the current players in the server (not including connecting clients). This function returns bots as well as human players. See player. GetBots and player. GetHumans. This function returns a sequential table, meaning it should be looped with ipairs instead of pairs for efficiency reasons.
table player.GetBots()
Returns a table of all bots on the server.
Player player.GetByAccountID( number accountID )
Tried to get the player with the specified Player:AccountID. Internally this function iterates over all players in the server, meaning it can be quite expensive in a performance-critical context.
Player player.GetByID( number connectionID )
Gets the player with the specified connection ID. Connection ID can be retrieved via gameevent. Listen events. For a function that returns a player based on their Entity:EntIndex, see Entity. For a function that returns a player based on their Player:UserID, see Player.
Player player.GetBySteamID( string steamID )
Gets the player with the specified SteamID. Internally this function iterates over all players in the server, meaning it can be quite expensive in a performance-critical context.
Player player.GetBySteamID64( string steamID64 )
Gets the player with the specified SteamID64. Internally this function iterates over all players in the server, meaning it can be quite expensive in a performance-critical context.
Player player.GetByUniqueID( string uniqueID )
We advise against using this. It may be changed or removed in a future update. Use player. GetBySteamID64, player. GetBySteamID or player. GetByAccountID to get a player by a unique identifier instead. Gets the player with the specified uniqueID (not recommended way to identify players). It is highly recommended to use player. GetByAccountID, player. GetBySteamID or player. GetBySteamID64 instead as this function can have collisions ( be same for different people ) while SteamID is guaranteed to unique to each player. Internally this function iterates over all players in the server, meaning it can be quite expensive in a performance-critical context.
number player.GetCount()
Gives you the player count. Similar to #player. GetAll() but with better performance since the player table doesn't have to be generated. If player. GetAll is already being called for iteration, then using the # operator on the table will be faster than calling this function since it is JITted.
table player.GetHumans()
Returns a table containing all human players (non-bot/AI). Unlike player. GetAll, this does not include bots. This function returns a sequential table, meaning it should be looped with ipairs instead of pairs for efficiency reasons.
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 exist entirely within lua, as opposed to player. GetAll, which is a C++ function. Because switching from lua to C++ (and vice versa) incurs a performance cost, this function will be somewhat more efficient than player. GetAll. The GM:OnEntityCreated and GM:EntityRemoved hooks are used internally to invalidate this function's cache. Using this function inside those hooks is not guaranteed to use an up-to-date cache because hooks are currently executed in an arbitrary order. An error being thrown inside the GM:OnEntityCreated or GM:EntityRemoved hooks is likely to break this function. Make it certain that no addons are causing any errors in those hooks.