GM/OnPlayerChat

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (Added a note to the example from before)
m (Changed the example a bit (again))
Line 68: Line 68:
 
{{Example
 
{{Example
 
|Description=How you could create a chat command
 
|Description=How you could create a chat command
|Code=hook.Add( 'OnPlayerChat', 'UNIQUE_IDENTIFIER', function( ply, txt, bTeam, bDead )
+
|Code=hook.Add( 'OnPlayerChat', 'UNIQUE_IDENTIFIER', function( ply, txt, bTeam, bDead )  
  
  if txt == '/addhealth' then -- if the player typed /addhealth
+
  txt = string.lower( txt ) -- make the string lower case
 
+
  ply:SetHealth( ply:Health() + 10 ) -- add some health to the player who typed this
+
 
+
  return true -- this suppresses the message from being shown
+
  
 +
if txt == '/addhealth' then -- if the player typed /addhealth then
 +
  ply:SetHealth( ply:Health() + 10 ) -- add some health to the player who typed this
 +
  return true -- this suppresses the message from being shown
 
  end
 
  end
  
 
end )
 
end )
 
}}
 
}}

Revision as of 05:35, 18 February 2016

 GM:OnPlayerChat( )

Contents

Description

Called whenever another player send a chat message. For the serverside equivalent, see GM:PlayerSay.

Arguments

Player ply

The player

Arguments

string text

The players chatted text

Arguments

boolean teamChat

Is the player typing in team chat?

Arguments

boolean isDead

Is the player dead?

Returns

boolean

Should the message be suppressed?

Examples

Example

function GM:OnPlayerChat( player, strText, bTeamOnly, bPlayerIsDead )
 
 --
 -- I've made this all look more complicated than it is. Here's the easy version
 --
 -- chat.AddText( player, Color( 255, 255, 255 ), ": ", strText )
 --
 
 local tab = {}
 
 if ( bPlayerIsDead ) then
 table.insert( tab, Color( 255, 30, 40 ) )
 table.insert( tab, "*DEAD* " )
 end
 
 if ( bTeamOnly ) then
 table.insert( tab, Color( 30, 160, 40 ) )
 table.insert( tab, "(TEAM) " )
 end
 
 if ( IsValid( player ) ) then
 table.insert( tab, player )
 else
 table.insert( tab, "Console" )
 end
 
 table.insert( tab, Color( 255, 255, 255 ) )
 table.insert( tab, ": "..strText )
 
 chat.AddText( unpack(tab) )

 return true
 
end


Examples

Example

How you could create a chat command

hook.Add( 'OnPlayerChat', 'UNIQUE_IDENTIFIER', function( ply, txt, bTeam, bDead ) 

 txt = string.lower( txt ) -- make the string lower case

 if txt == '/addhealth' then -- if the player typed /addhealth then
   ply:SetHealth( ply:Health() + 10 ) -- add some health to the player who typed this
   return true -- this suppresses the message from being shown
 end

end )


Personal tools
Navigation