OnPlayerChat: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Fixed incorrect argument provided to an example (nearbyPlayer))
 
(7 intermediate revisions by 3 users not shown)
Line 10: Line 10:
*'''message''': a [[string]] representing the message typed into the chat.
*'''message''': a [[string]] representing the message typed into the chat.
*'''messageType''': an [[int]] value representing the message type:
*'''messageType''': an [[int]] value representing the message type:
**'''0''': normal message
{{Message Types}}
**'''1''': action message (/me)
**'''2''': team message


==Source==
==Source==
Line 24: Line 22:


<section name="Example 1" class="server" show="true">
<section name="Example 1" class="server" show="true">
This example limits receiving of chat messages to a spherical area around the player who sent the message, also blocking action and team text.
This example limits receiving of chat messages to area around the player who sent the message, also blocking action and team text.


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- define our chat radius
local chatRadius = 20 -- define our chat radius
local chatRadius = 20 --units


-- define a handler that will distribute the message to all nearby players
function onPlayerChatSendMessageToNearbyPlayers(messageText, messageType)
function sendMessageToNearbyPlayers(message, messageType)
local normalMessage = (messageType == 0) -- we will only send normal chat messages, action and team types will be ignored
    -- we will only send normal chat messages, action and team types will be ignored
    if messageType == 0 then
        -- get the chatting player's position
        local posX1, posY1, posZ1 = getElementPosition(source)


        -- loop through all player and check distance
if (not normalMessage) then -- it's not normal message
        for id, player in ipairs(getElementsByType("player")) do
return false -- do not continue
            local posX2, posY2, posZ2 = getElementPosition(player)
end
            if getDistanceBetweenPoints3D(posX1, posY1, posZ1, posX2, posY2, posZ2) <= chatRadius then
 
                outputChatBox(message, player)
local playerName = getPlayerName(source)
            end
local playerX, playerY, playerZ = getElementPosition(source) -- get position of player who sent the message
        end
local playerInterior = getElementInterior(source) -- get interior of same player
    end
local playerDimension = getElementDimension(source) -- dimension as well
    -- block the original message by cancelling this event
local nearbyPlayers = getElementsWithinRange(playerX, playerY, playerZ, chatRadius, "player", playerInterior, playerDimension) -- get nearby players within given radius
    cancelEvent()
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
end
-- attach our new chat handler to onPlayerChat
addEventHandler("onPlayerChat", root, onPlayerChatSendMessageToNearbyPlayers)
addEventHandler( "onPlayerChat", getRootElement(), sendMessageToNearbyPlayers )
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>


<section name="Example 2" class="server" show="true">
<section name="Example 2" class="server" show="true">
Line 60: Line 53:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--This function is executed when a player joins, it sets the player's name-tag color to a random color.
--This function is executed when a player joins, it sets the player's name-tag color to a random color.
local function playerJoin()
local function playerJoin()
local red, green, blue = math.random (50, 255), math.random (50, 255), math.random (50, 255)
local red, green, blue = math.random (50, 255), math.random (50, 255), math.random (50, 255)
Line 67: Line 61:


--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.
--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.
local function playerChat(message, messageType)
local function playerChat(message, messageType)
if messageType == 0 then --Global (main) chat
if messageType == 0 then --Global (main) chat
Line 72: Line 67:
                 local red, green, blue = getPlayerNametagColor(source)
                 local red, green, blue = getPlayerNametagColor(source)
outputChatBox(getPlayerName(source)..": #FFFFFF"..message, root, red, green, blue, true )
outputChatBox(getPlayerName(source)..": #FFFFFF"..message, root, red, green, blue, true )
outputServerLog("CHAT: "..getPlayerName(source)..": "..message)--NOTE: Beacuse we cancelled the onPlayerChat event, we need to log chat manually.
outputServerLog("CHAT: "..getPlayerName(source)..": "..message) -- Because we cancelled the onPlayerChat event, we need to log chat manually.
end
end
end
end
Line 88: Line 83:
         killPed ( source, source ) -- Kills that player that typed the string 'kill'
         killPed ( source, source ) -- Kills that player that typed the string 'kill'
     end
     end
end --End of the function
end
addEventHandler("onPlayerChat", getRootElement(), onChat)
addEventHandler("onPlayerChat", root, onChat)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Latest revision as of 16:02, 3 May 2024

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

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

Cancel effect

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.

Examples

Click to collapse [-]
Example 1

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)
Click to collapse [-]
Example 2

This example implements colored player names in chat.

--This function is executed when a player joins, it sets the player's name-tag color to a random color.

local function playerJoin()
	local red, green, blue = math.random (50, 255), math.random (50, 255), math.random (50, 255)
        setPlayerNametagColor(source, red, green, blue)
end
addEventHandler ("onPlayerJoin", root, playerJoin)

--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.

local function playerChat(message, messageType)
	if messageType == 0 then --Global (main) chat
                cancelEvent()
                local red, green, blue = getPlayerNametagColor(source)
		outputChatBox(getPlayerName(source)..": #FFFFFF"..message, root, red, green, blue, true )
		outputServerLog("CHAT: "..getPlayerName(source)..": "..message) -- Because we cancelled the onPlayerChat event, we need to log chat manually.
	end
end
addEventHandler("onPlayerChat", root, playerChat)


Click to collapse [-]
Example 3

This is a script that kills any player that says 'kill'.

function onChat(message, messageType)
    if string.find(message, 'kill') then  -- Searches for the string 'kill' in the message sent
        killPed ( source, source ) -- Kills that player that typed the string 'kill'
    end
end
addEventHandler("onPlayerChat", root, onChat)

See Also

Player events


Event functions

Shared