OutputChatBox: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
(41 intermediate revisions by 29 users not shown)
Line 1: Line 1:
{{Server client function}}
{{Server client function}}
{{Note|Avoid outputting text to the chatbox that isn't actually chat, as this can be annoying for players. Output information and status messages to the HUD.}}
__NOTOC__
__NOTOC__
This outputs the specified text string to the chatbox. It can be specified as a message to certain player(s) or all players.  
This outputs the specified text string to the chatbox. It can be specified as a message to certain player(s) or all players.
 
It can optionally allow you to embed color changes into the string by setting the colorCoded boolean to true. This allows:
<syntaxhighlight lang="lua">
outputChatBox ( "#FF0000Hello #00FF00World", root, 255, 255, 255, true )
</syntaxhighlight>
This will display as: '''<span style='color:red;'>Hello</span> <span style='color:green'>World</span> '''


==Syntax==
==Syntax==
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">bool outputChatBox ( string text, [ element visibleTo=getRootElement(), int r=255, int g=255, int b=255, bool colorCoded=false ] )</syntaxhighlight>
<syntaxhighlight lang="lua">bool outputChatBox ( string text [, element visibleTo = root, int r = 231, int g = 217, int b = 176, bool colorCoded = false ] )</syntaxhighlight>
 
{{OOP||[[player]]:outputChat}}
==Required Arguments==
==Required Arguments==
*'''text:''' The text string that you wish to send to the chat window.
*'''text:''' The text string that you wish to send to the chat window. If more than 256 characters it will not be showed in chat.


==Optional Arguments==
==Optional Arguments==
{{OptionalArg}}
{{OptionalArg}}
*'''visibleTo:''' This specifies who the chat is visible to. Any players in this element will see the chat message. See [[visibility]].
*'''visibleTo:''' This specifies who the chat is visible to. Any players in this element will see the chat message. See [[visibility]].
*'''r:''' The amount of red in the color of the text. Default value is 255.
{{New items|5.0157|1.5.7-9.20391|
*'''g:''' The amount of green in the color of the text. Default value is 255.
*'''visibleTo:''' Can also be a table of players or team.}}
*'''b:''' The amount of blue in the color of the text. Default value is 255.
*'''r:''' The amount of red in the color of the text. Default value is 231.
*'''g:''' The amount of green in the color of the text. Default value is 217.
*'''b:''' The amount of blue in the color of the text. Default value is 176.
*'''colorCoded:''' A boolean value determining whether or not '#RRGGBB' tags should be used.
*'''colorCoded:''' A boolean value determining whether or not '#RRGGBB' tags should be used.
Note: '''visibleTo''' can also be a Team object, in this case, the text will be visible to all the players of that team.
Note: The #RRGGBB format must contain capital letters a-f is not acceptable but A-F is. Default RGB values in this format are: '#E7D9B0'.
</section>
</section>
<section name="Client" class="client" show="false">
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">bool outputChatBox ( string text, [ int r=255, int g=255, int b=255, bool colorCoded=false ] )</syntaxhighlight>
<syntaxhighlight lang="lua">bool outputChatBox ( string text [, int r = 231, int g = 217, int b = 176, bool colorCoded = false ] )</syntaxhighlight>


==Required Arguments==
==Required Arguments==
*'''text:''' The text string that you wish to send to the chat window.
*'''text:''' The text string that you wish to send to the chat window. If more than 256 characters it will not be showed in chat.


==Optional Arguments==
==Optional Arguments==
{{OptionalArg}}
{{OptionalArg}}
*'''r:''' The amount of red in the color of the text. Default value is 255.
*'''r:''' The amount of red in the color of the text. Default value is 231.
*'''g:''' The amount of green in the color of the text. Default value is 255.
*'''g:''' The amount of green in the color of the text. Default value is 217.
*'''b:''' The amount of blue in the color of the text. Default value is 255.
*'''b:''' The amount of blue in the color of the text. Default value is 176.
*'''colorCoded:''' A boolean value determining whether or not '#RRGGBB' tags should be used.
*'''colorCoded:''' A boolean value determining whether or not '#RRGGBB' tags should be used.
Note: '''visibleTo''' can also be a Team object, in this case, the text will be visible to all the players of that team.
Note: The #RRGGBB format must contain capital letters a-f is not acceptable but A-F is. Default RGB values in this format are: '#E7D9B0'.
 
</section>
</section>


Line 49: Line 59:
'''Example 2:''' This example outputs a simple colour coded message, "Red White", where the 'White' is in white colour, and 'Red' is in a red colour.
'''Example 2:''' This example outputs a simple colour coded message, "Red White", where the 'White' is in white colour, and 'Red' is in a red colour.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
  outputChatBox ( "Red #ffffffWhite", 255,0,0,true )
  outputChatBox ( "Red #FFFFFFWhite", getRootElement(), 255, 0, 0, true )
</syntaxhighlight>
</syntaxhighlight>


Line 59: Line 69:
message = string.gsub(message, "#%x%x%x%x%x%x", "") --remove any hex tags in a player's chat to prevent custom colours by using lua's string.gsub
message = string.gsub(message, "#%x%x%x%x%x%x", "") --remove any hex tags in a player's chat to prevent custom colours by using lua's string.gsub
local r,g,b = getPlayerNametagColor ( source ) --get the player's nametag colour
local r,g,b = getPlayerNametagColor ( source ) --get the player's nametag colour
local chatterName = getClientName ( source ) --get his name
local chatterName = getPlayerName ( source ) --get his name
--output a message with the name as his nametag colour, and the rest in white.
--output a message with the name as his nametag colour, and the rest in white.
outputChatBox ( chatterName..":#FFFFFF "..message, getRootElement(), r, g, b, true )
outputChatBox ( chatterName..":#FFFFFF "..message, root, r, g, b, true )
end
end
end
end
addEventHandler("onPlayerChat", root, colouredChat)
</syntaxhighlight>
</syntaxhighlight>


Line 69: Line 80:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Find the player element for the player called 'someguy'
-- Find the player element for the player called 'someguy'
myPlayer = getPlayerFromNick ( "someguy" )
myPlayer = getPlayerFromName ( "someguy" )
-- If a player was found called 'someguy' then...
-- If a player was found called 'someguy' then...
if ( myPlayer ~= false ) then
if ( myPlayer ~= false ) then
Line 77: Line 88:
     outputChatBox ( "I have " .. x .. " apples and " .. y .. " oranges.", myPlayer )
     outputChatBox ( "I have " .. x .. " apples and " .. y .. " oranges.", myPlayer )
end
end
</syntaxhighlight>
'''Example 5:''' These two functions can speed up typing, and display a message when a player Joins.
<syntaxhighlight lang="lua">
local msg_red,msg_green,msg_blue = 255,255,0
function servertalkprivate(message, sendto)
        --Talk to one client only
outputChatBox(tostring(message), sendto, msg_red, msg_green, msg_blue, true)
end
function servertalk(message)
        --Talk to everyone
servertalkprivate(message, root)
end
function onJoin()
servertalkprivate("Welcome to My Server", source)
end
addEventHandler("onPlayerJoin",root,onJoin)
</syntaxhighlight>
'''Example 6:''' This can be used to display a message when the player joins and sets its armor to 100.
<syntaxhighlight lang="lua">
function onJoin()
        setPedArmor(source, 100)
        local playerName = getPlayerName(source)
        outputChatBox("Welcome ".. playerName .." To The Server", source, 0, 154, 255)
end
addEventHandler("onPlayerJoin", root, onJoin)
</syntaxhighlight>
'''Example 7:''' This code will output an message to nearby players.
<syntaxhighlight lang="lua">
function displayLocalMessage(player, cmd)
if player then -- Check if command was triggered by player
local x, y, z = getElementPosition(player) -- Get player position
local nearbyPlayers = getElementsWithinRange(x, y, z, 10, "player") -- Retrieve nearby players in range of 10
outputChatBox("Local message :)!", nearbyPlayers) -- Output our message
end
end
addCommandHandler("localmessage", displayLocalMessage)
</syntaxhighlight>
'''Example 8:''' This example displays your nickname and the amount of money.
<syntaxhighlight lang="lua">
function player_info(player,cmd)
outputChatBox("Name: #FFFFFF"..getPlayerName(player),player,255,0,0,true)
outputChatBox("Money: #FFFFFF"..getPlayerMoney(player).."#00FF00$",player,255,0,0,true)
end
addCommandHandler("info",player_info)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
Line 82: Line 146:
==See Also==
==See Also==
{{Server functions}}
{{Server functions}}
[[cs:outputChatBox]]
[[pl:outputChatBox]]
[[ru:outputChatBox]]
[[pt-br:OutputChatBox]]

Revision as of 08:50, 4 November 2020

[[{{{image}}}|link=|]] Note: Avoid outputting text to the chatbox that isn't actually chat, as this can be annoying for players. Output information and status messages to the HUD.

This outputs the specified text string to the chatbox. It can be specified as a message to certain player(s) or all players.

It can optionally allow you to embed color changes into the string by setting the colorCoded boolean to true. This allows:

outputChatBox ( "#FF0000Hello #00FF00World", root, 255, 255, 255, true )

This will display as: Hello World

Syntax

Click to collapse [-]
Server
bool outputChatBox ( string text [, element visibleTo = root, int r = 231, int g = 217, int b = 176, bool colorCoded = false ] )

OOP Syntax Help! I don't understand this!

Method: player:outputChat(...)


Required Arguments

  • text: The text string that you wish to send to the chat window. If more than 256 characters it will not be showed in chat.

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • visibleTo: This specifies who the chat is visible to. Any players in this element will see the chat message. See visibility.
ADDED/UPDATED IN VERSION 1.5.7-9.20391 :
  • visibleTo: Can also be a table of players or team.
  • r: The amount of red in the color of the text. Default value is 231.
  • g: The amount of green in the color of the text. Default value is 217.
  • b: The amount of blue in the color of the text. Default value is 176.
  • colorCoded: A boolean value determining whether or not '#RRGGBB' tags should be used.
  • Note: The #RRGGBB format must contain capital letters a-f is not acceptable but A-F is. Default RGB values in this format are: '#E7D9B0'.

    Click to collapse [-]
    Client
    bool outputChatBox ( string text [, int r = 231, int g = 217, int b = 176, bool colorCoded = false ] )

    Required Arguments

    • text: The text string that you wish to send to the chat window. If more than 256 characters it will not be showed in chat.

    Optional Arguments

    NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

    • r: The amount of red in the color of the text. Default value is 231.
    • g: The amount of green in the color of the text. Default value is 217.
    • b: The amount of blue in the color of the text. Default value is 176.
    • colorCoded: A boolean value determining whether or not '#RRGGBB' tags should be used.

    Note: The #RRGGBB format must contain capital letters a-f is not acceptable but A-F is. Default RGB values in this format are: '#E7D9B0'.

    Returns

    Returns true if the message was displayed successfully. Returns false if invalid arguments are specified.

    Example

    Click to collapse [-]
    Server

    Example 1: This example displays a chat message to all users.

    x = 5
    y = 10  
    -- Displays the message
    outputChatBox ( "I have " .. x .. " apples and " .. y .. " oranges." )
    

    Example 2: This example outputs a simple colour coded message, "Red White", where the 'White' is in white colour, and 'Red' is in a red colour.

     outputChatBox ( "Red #FFFFFFWhite", getRootElement(), 255, 0, 0, true )
    

    Example 3: This example allows for coloured chat, according to a player's nametag. This makes use of colour coded outputs.

    function colouredChat ( message, theType )
    	if theType == 0 then --if its normal chat (not /me or teamchat) then
    		cancelEvent() --prevent MTA from outputting chat
    		message = string.gsub(message, "#%x%x%x%x%x%x", "") --remove any hex tags in a player's chat to prevent custom colours by using lua's string.gsub
    		local r,g,b = getPlayerNametagColor ( source ) --get the player's nametag colour
    		local chatterName = getPlayerName ( source ) --get his name
    		--output a message with the name as his nametag colour, and the rest in white.
    		outputChatBox ( chatterName..":#FFFFFF "..message, root, r, g, b, true )
    	end
    end
    addEventHandler("onPlayerChat", root, colouredChat)
    

    Example 4: This example displays a chat message to a single user called someguy.

    -- Find the player element for the player called 'someguy'
    myPlayer = getPlayerFromName ( "someguy" )
    -- If a player was found called 'someguy' then...
    if ( myPlayer ~= false ) then
        x = 5
        y = 10
        -- Display the message
        outputChatBox ( "I have " .. x .. " apples and " .. y .. " oranges.", myPlayer )
    end
    

    Example 5: These two functions can speed up typing, and display a message when a player Joins.

    local msg_red,msg_green,msg_blue = 255,255,0
    
    function servertalkprivate(message, sendto)
            --Talk to one client only
    	outputChatBox(tostring(message), sendto, msg_red, msg_green, msg_blue, true)
    end
    
    function servertalk(message)
            --Talk to everyone
    	servertalkprivate(message, root)
    end
    
    function onJoin()
    	servertalkprivate("Welcome to My Server", source)
    end
    
    addEventHandler("onPlayerJoin",root,onJoin)
    

    Example 6: This can be used to display a message when the player joins and sets its armor to 100.

    function onJoin()
             setPedArmor(source, 100)
             local playerName = getPlayerName(source)
             outputChatBox("Welcome ".. playerName .." To The Server", source, 0, 154, 255)
    end
    addEventHandler("onPlayerJoin", root, onJoin)
    

    Example 7: This code will output an message to nearby players.

    function displayLocalMessage(player, cmd)
    	if player then -- Check if command was triggered by player
    		local x, y, z = getElementPosition(player) -- Get player position
    		local nearbyPlayers = getElementsWithinRange(x, y, z, 10, "player") -- Retrieve nearby players in range of 10
    
    		outputChatBox("Local message :)!", nearbyPlayers) -- Output our message
    	end
    end
    addCommandHandler("localmessage", displayLocalMessage)
    

    Example 8: This example displays your nickname and the amount of money.

    function player_info(player,cmd)
    	outputChatBox("Name: #FFFFFF"..getPlayerName(player),player,255,0,0,true)
    	outputChatBox("Money: #FFFFFF"..getPlayerMoney(player).."#00FF00$",player,255,0,0,true)
    end
    addCommandHandler("info",player_info)
    

    See Also