onPlayerChat | Multi Theft Auto: Wiki Skip to content

onPlayerChat

Client-side
Server-side
Shared

This event is triggered when a player chats inside the chatbox.

Parameters

string message, int messageType
  • message: a string representing the message typed into the chat.
  • messageType: an int value representing the message type: 0: normal message 1: action message (/me) 2: team message 3: private message 4: internal message

Source

element: The source of this event is the player who sent the chatbox message.

Canceling

If this event is canceled, the game's chat system won't deliver the posts. You may use outputChatBox to send the messages then. 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.

Code Examples

server

This example limits receiving of chat messages to area around the player who sent the message, also blocking action and team text.

local chatRadius = 20 -- define our chat radius
function onPlayerChatSendMessageToNearbyPlayers(messageText, messageType)
local normalMessage = (messageType == 0) -- we will only send normal chat messages, action and team types will be ignored
if (not normalMessage) then -- it's not normal message
return false -- do not continue
end
local playerName = getPlayerName(source)
local playerX, playerY, playerZ = getElementPosition(source) -- get position of player who sent the message
local playerInterior = getElementInterior(source) -- get interior of same player
local playerDimension = getElementDimension(source) -- dimension as well
local nearbyPlayers = getElementsWithinRange(playerX, playerY, playerZ, chatRadius, "player", playerInterior, playerDimension) -- get nearby players within given radius
local messageToOutput = playerName..": "..messageText
outputChatBox(messageToOutput, nearbyPlayers, 255, 255, 255, true) -- output message to them
cancelEvent() -- block the original message by cancelling this event
end
addEventHandler("onPlayerChat", root, onPlayerChatSendMessageToNearbyPlayers)

See Also