<?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=John+Michael</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=John+Michael"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/wiki/Special:Contributions/John_Michael"/>
	<updated>2026-04-26T09:38:03Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OnPlayerChat&amp;diff=27299</id>
		<title>OnPlayerChat</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OnPlayerChat&amp;diff=27299"/>
		<updated>2011-10-08T21:34:56Z</updated>

		<summary type="html">&lt;p&gt;John Michael: Cleaned up the second example, made it work as described.&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;
==Example== &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;
&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;
This example sets random color to every player who joins.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- sets colors when player join&lt;br /&gt;
function onJoin ()&lt;br /&gt;
	local g_Red, g_Green, g_Blue = math.random (50, 255), math.random (50, 255), math.random (50, 255)&lt;br /&gt;
        setPlayerNametagColor(source, g_Red, g_Green, g_Blue)&lt;br /&gt;
end&lt;br /&gt;
-- checks if player has sent a message&lt;br /&gt;
function onChat ( message, messageType )&lt;br /&gt;
	if messageType == 0 then&lt;br /&gt;
                cancelEvent()&lt;br /&gt;
                local g_Red, g_Green, g_Blue = getPlayerNametagColor(source)&lt;br /&gt;
		outputChatBox ( getPlayerName ( source ) .. &amp;quot;: #E0D0B0&amp;quot; .. message, root, g_Red, g_Green, g_Blue, true )&lt;br /&gt;
		outputServerLog( &amp;quot;CHAT: &amp;quot; .. getPlayerName ( source ).. &amp;quot;: &amp;quot; .. message )&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
--root is a constant variable for getRootElement() - so these events will all be attatched to the root element.&lt;br /&gt;
addEventHandler ( &amp;quot;onPlayerJoin&amp;quot;, root, onJoin)&lt;br /&gt;
addEventHandler ( &amp;quot;onPlayerChat&amp;quot;, root, onChat )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{See also/Server event|Player events}}&lt;/div&gt;</summary>
		<author><name>John Michael</name></author>
	</entry>
</feed>