<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Aimcac</id>
	<title>Multi Theft Auto: Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Aimcac"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/wiki/Special:Contributions/Aimcac"/>
	<updated>2026-04-27T00:42:16Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OnPlayerChat&amp;diff=49869</id>
		<title>OnPlayerChat</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OnPlayerChat&amp;diff=49869"/>
		<updated>2016-11-23T17:12:25Z</updated>

		<summary type="html">&lt;p&gt;Aimcac: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__NOTOC__&lt;br /&gt;
{{Server event}}&lt;br /&gt;
This event is triggered when a player chats inside the chat box.&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
string message, int messageType&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
*'''message''': A string representing the message typed into the chat.&lt;br /&gt;
*'''messageType''': An integer value representing the message type:&lt;br /&gt;
**'''0''': normal message&lt;br /&gt;
**'''1''': action message (/me)&lt;br /&gt;
**'''2''': team message&lt;br /&gt;
&lt;br /&gt;
==Source==&lt;br /&gt;
The [[event system#Event source|source]] of this event is the [[player]] who sent the chatbox message.&lt;br /&gt;
&lt;br /&gt;
==Cancel effect==&lt;br /&gt;
If this event is [[Event system#Canceling|canceled]], the game's chat system won't deliver the posts. You may use [[outputChatBox]] to send the messages then.&lt;br /&gt;
Cancelling this event also means the chat will not appear in the server console or logs. If you want chat logging, you will have to add a call to [[outputServerLog]] - See the second example.&lt;br /&gt;
&lt;br /&gt;
==Examples== &lt;br /&gt;
&lt;br /&gt;
&amp;lt;section name=&amp;quot;Example 1&amp;quot; class=&amp;quot;server&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
This example limits receiving of chat messages to a spherical area around the player who sent the message, also blocking action and team text.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- define our chat radius&lt;br /&gt;
local chatRadius = 20 --units&lt;br /&gt;
&lt;br /&gt;
-- define a handler that will distribute the message to all nearby players&lt;br /&gt;
function sendMessageToNearbyPlayers( message, messageType )&lt;br /&gt;
    -- we will only send normal chat messages, action and team types will be ignored&lt;br /&gt;
    if messageType == 0 then&lt;br /&gt;
        -- get the chatting player's position&lt;br /&gt;
        local posX, posY, posZ = getElementPosition( source )&lt;br /&gt;
        &lt;br /&gt;
        -- create a sphere of the specified radius in that position&lt;br /&gt;
        local chatSphere = createColSphere( posX, posY, posZ, chatRadius )&lt;br /&gt;
        -- get a table all player elements inside it&lt;br /&gt;
        local nearbyPlayers = getElementsWithinColShape( chatSphere, &amp;quot;player&amp;quot; )&lt;br /&gt;
        -- and destroy the sphere, since we're done with it&lt;br /&gt;
        destroyElement( chatSphere )&lt;br /&gt;
        &lt;br /&gt;
        -- deliver the message to each player in that table&lt;br /&gt;
        for index, nearbyPlayer in ipairs( nearbyPlayers ) do&lt;br /&gt;
            outputChatBox( message, nearbyPlayer )&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
-- attach our new chat handler to onPlayerChat&lt;br /&gt;
addEventHandler( &amp;quot;onPlayerChat&amp;quot;, getRootElement(), sendMessageToNearbyPlayers )&lt;br /&gt;
&lt;br /&gt;
-- define another handler function that cancels the event so that the message won't be delivered through the &lt;br /&gt;
function blockChatMessage()&lt;br /&gt;
    cancelEvent()&lt;br /&gt;
end&lt;br /&gt;
-- attach it as a handler to onPlayerChat&lt;br /&gt;
addEventHandler( &amp;quot;onPlayerChat&amp;quot;, getRootElement(), blockChatMessage )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;section name=&amp;quot;Example 2&amp;quot; class=&amp;quot;server&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
This example implements colored player names in chat.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
--This function is executed when a player joins, it sets the player's name-tag color to a random color.&lt;br /&gt;
local function playerJoin()&lt;br /&gt;
	local red, green, blue = math.random (50, 255), math.random (50, 255), math.random (50, 255)&lt;br /&gt;
        setPlayerNametagColor(source, red, green, blue)&lt;br /&gt;
end&lt;br /&gt;
addEventHandler (&amp;quot;onPlayerJoin&amp;quot;, root, playerJoin)&lt;br /&gt;
&lt;br /&gt;
--This function is executed when a player says something in chat, it outputs the player's message, with their nick colored to match their name tag color.&lt;br /&gt;
local function playerChat(message, messageType)&lt;br /&gt;
	if messageType == 0 then --Global (main) chat&lt;br /&gt;
                cancelEvent()&lt;br /&gt;
                local red, green, blue = getPlayerNametagColor(source)&lt;br /&gt;
		outputChatBox(getPlayerName(source)..&amp;quot;: #FFFFFF&amp;quot;..message, root, red, green, blue, true )&lt;br /&gt;
		outputServerLog(&amp;quot;CHAT: &amp;quot;..getPlayerName(source)..&amp;quot;: &amp;quot;..message)--NOTE: Beacuse we cancelled the onPlayerChat event, we need to log chat manually.&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
addEventHandler(&amp;quot;onPlayerChat&amp;quot;, root, playerChat)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;section name=&amp;quot;Example 3&amp;quot; class=&amp;quot;server&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
This is a script that slaps any player that says slap.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
function slap(thePlayer, msg)&lt;br /&gt;
    if string.find(msg, 'slap') then  -- Searches for the string 'slap' in the message sent&lt;br /&gt;
        killPed (thePlayer, thePlayer) -- Kills that player that typed the string 'slap'&lt;br /&gt;
    end&lt;br /&gt;
end --End of the function&lt;br /&gt;
&lt;br /&gt;
addEventHandler(&amp;quot;onPlayerChat&amp;quot;, getRootElement(), slap)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{See also/Server event|Player events}}&lt;/div&gt;</summary>
		<author><name>Aimcac</name></author>
	</entry>
</feed>