OnPlayerChat: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
Line 88: Line 88:


<section name="Example 3" class="server" show="true">
<section name="Example 3" class="server" show="true">
This is a script for slap the player by a word in the chat.
This is a script that slaps any player that says slap.


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">


function Slap(thePlayer, msg)
function slap(thePlayer, msg)
if string.find(msg, 'slap') then  --Here we have put the word or the msg that will be used to slap the player
    if string.find(msg, 'slap') then  -- Searches for the string 'slap' in the message sent
killPed (thePlayer, thePlayer) --This the action >> killing the player
        killPed (thePlayer, thePlayer) -- Kills that player that typed the string 'slap'
end     --End if
    end
end     --End function
end --End of the function
addEventHandler("onPlayerChat", getRootElement(), Slap)
 
addEventHandler("onPlayerChat", getRootElement(), slap)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>


{{See also/Server event|Player events}}
{{See also/Server event|Player events}}

Revision as of 17:12, 23 November 2016

This event is triggered when a player chats inside the chat box.

Parameters

string message, int messageType
  • message: A string representing the message typed into the chat.
  • messageType: An integer value representing the message type:
    • 0: normal message
    • 1: action message (/me)
    • 2: team 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 a spherical area around the player who sent the message, also blocking action and team text.


-- define our chat radius
local chatRadius = 20 --units

-- define a handler that will distribute the message to all nearby players
function sendMessageToNearbyPlayers( message, messageType )
    -- 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 posX, posY, posZ = getElementPosition( source )
        
        -- create a sphere of the specified radius in that position
        local chatSphere = createColSphere( posX, posY, posZ, chatRadius )
        -- get a table all player elements inside it
        local nearbyPlayers = getElementsWithinColShape( chatSphere, "player" )
        -- and destroy the sphere, since we're done with it
        destroyElement( chatSphere )
        
        -- deliver the message to each player in that table
        for index, nearbyPlayer in ipairs( nearbyPlayers ) do
            outputChatBox( message, nearbyPlayer )
        end
    end
end
-- attach our new chat handler to onPlayerChat
addEventHandler( "onPlayerChat", getRootElement(), sendMessageToNearbyPlayers )

-- define another handler function that cancels the event so that the message won't be delivered through the 
function blockChatMessage()
    cancelEvent()
end
-- attach it as a handler to onPlayerChat
addEventHandler( "onPlayerChat", getRootElement(), blockChatMessage )
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)--NOTE: Beacuse 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 slaps any player that says slap.


function slap(thePlayer, msg)
    if string.find(msg, 'slap') then  -- Searches for the string 'slap' in the message sent
        killPed (thePlayer, thePlayer) -- Kills that player that typed the string 'slap'
    end
end --End of the function

addEventHandler("onPlayerChat", getRootElement(), slap)

See Also

Player events


Event functions