Garry's Mod Wiki

GM:OnPlayerChat

boolean GM:OnPlayerChat( Player ply, string text, boolean teamChat, boolean isDead )

Description

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

The text input of this hook depends on GM:PlayerSay. If it is suppressed on the server, it will be suppressed on the client. This also means, that a message surpressed with this hook will be still visible to other clients.

Arguments

1 Player ply
The player
2 string text
The message's text
3 boolean teamChat
Is the player typing in team chat?
4 boolean isDead
Is the player dead?

Returns

1 boolean
Should the message be suppressed?

Example

Code from base gamemode. See garrysmod/gamemodes/base/gamemode/cl_init.lua#L139

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

Example

How you could create a clientside only chat command.

hook.Add( "OnPlayerChat", "HelloCommand", function( ply, strText, bTeam, bDead ) if ( ply != LocalPlayer() ) then return end strText = string.lower( strText ) -- make the string lower case if (strText == "/hello") then -- if the player typed /hello then print("Hello world!") -- print Hello world to the console return true -- this suppresses the message from being shown end end )
Output: Prints "Hello world!" to the console when you type /hello in the chat.

Page Links


Special Pages


Wikis

?

Render Time: 51ms

Session 0
DB GetPage 3
Generate Html 25
SaveChanges 9
Render Body 0
Render Sidebar 12