Garry's Mod Wiki

GM:PlayerSay

  string GM:PlayerSay( Player sender, string text, boolean teamChat )

Description

Called when a player dispatched a chat message. For the clientside equivalent, see GM:OnPlayerChat.

It may be more reliable to use gameevent/player_say to read messages serverside because addons commonly return values in this hook to change chat messages.

Arguments

1 Player sender
The player which sent the message.
2 string text
The message's content.
3 boolean teamChat
Return false when the message is for everyone, true when the message is for the sender's team.

Returns

1 string
What to show instead of original text. Set to "" to stop the message from displaying.

Example

Adds a coin flip command to the chat. Player should type /flip (case-insensitive).

hook.Add( "PlayerSay", "CoinFlip", function( ply, text ) if ( string.lower( text ) == "/flip" ) then PrintMessage( HUD_PRINTTALK, ply:Nick() .. " flipped the coin and got " .. ( math.random( 2 ) == 1 and "heads" or "tails" ) ) return "" end end )
Output:
Player1 flipped the coin and got heads

Example

Adds a symbol counter command to the chat. Player should type /len *text* (case-insensitive).

hook.Add( "PlayerSay", "CharCount", function( ply, text ) if ( string.StartWith( string.lower( text ), "/len " ) ) then ply:ChatPrint( "Your message has " .. utf8.len( string.sub( text, 6 ) ) .. " symbol(s)." ) -- We use 6 in string.sub because it's a length of "/len " + 1 return "" end end )
Output:
Your message has 5 symbol(s). [When someone typed: "/len Hello"]