GM/OnPlayerChat

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(fixed spelling mistakes, indented GM hook properly again, replaced example because previous one didn't work (hook is CLIENT))
m (Added note that the text input depends on the server)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{Hook
 
{{Hook
 
|Description=Called whenever a player sends a chat message. For the serverside equivalent, see {{HookFunction|GM|PlayerSay}}.
 
|Description=Called whenever a player sends a chat message. For the serverside equivalent, see {{HookFunction|GM|PlayerSay}}.
 +
{{Note|The text input of this hook depends on {{HookFunction|GM|PlayerSay}}. If it is suppressed on the server, it will be suppressed on the client.}}
 
|Realm=Client
 
|Realm=Client
 
|Predicted=No
 
|Predicted=No
|Name=OnPlayerChat
 
|Parent=GM
 
 
}}
 
}}
 
{{Arg
 
{{Arg
Line 31: Line 30:
 
}}
 
}}
 
{{Example
 
{{Example
 +
|Description=Code from base gamemode. See garrysmod/gamemodes/base/gamemode/cl_init.lua#L139
 
|Code=function GM:OnPlayerChat( player, strText, bTeamOnly, bPlayerIsDead )
 
|Code=function GM:OnPlayerChat( player, strText, bTeamOnly, bPlayerIsDead )
 
   
 
   
Line 67: Line 67:
 
}}
 
}}
 
{{Example
 
{{Example
|Description=How you could create a chat command
+
|Description=How you could create a clientside only chat command.
|Code=hook.Add( "OnPlayerChat", "UNIQUE_IDENTIFIER", function( ply, strText, bTeam, bDead )  
+
|Code=hook.Add( "OnPlayerChat", "HelloCommand", function( ply, strText, bTeam, bDead )  
 +
    if ( ply != LocalPlayer() ) then return end
  
 
strText = string.lower( strText ) -- make the string lower case
 
strText = string.lower( strText ) -- make the string lower case
  
if strText == "/hello" then -- if the player typed /addhealth then
+
if (strText == "/hello") then -- if the player typed /hello then
print("Hello world!")
+
print("Hello world!") -- print Hello world to the console
 
return true -- this suppresses the message from being shown
 
return true -- this suppresses the message from being shown
 
end
 
end

Latest revision as of 22:06, 17 March 2019

 GM:OnPlayerChat( )

Contents

Description

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

NOTE

The text input of this hook depends on GM:PlayerSay. If it is suppressed on the server, it will be suppressed on the client.

Arguments

Player ply

The player

Arguments

string text

The message's 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

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


Examples

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.
Personal tools
Navigation