OutputChatBox: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
Line 66: Line 66:
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, getRootElement(), r, g, b, true )

Revision as of 12:16, 27 August 2009

This template is no longer in use as it results in poor readability.

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", getRootElement(), 255, 255, 255, true )

This will display as: Hello World

Syntax

Click to collapse [-]
Server
bool outputChatBox ( string text [, element visibleTo=getRootElement(), int r=255, int g=255, int b=255, bool colorCoded=false ] )

Required Arguments

  • text: The text string that you wish to send to the chat window.

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.
  • r: The amount of red in the color of the text. Default value is 255.
  • g: The amount of green in the color of the text. Default value is 255.
  • b: The amount of blue in the color of the text. Default value is 255.
  • 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. Also the #RRGGBB format must contain capital letters a-f is not acceptable but A-F is.

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

Required Arguments

  • text: The text string that you wish to send to the chat window.

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 255.
  • g: The amount of green in the color of the text. Default value is 255.
  • b: The amount of blue in the color of the text. Default value is 255.
  • 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.

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, getRootElement(), r, g, b, true )
	end
end

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

-- Find the player element for the player called 'someguy'
myPlayer = getPlayerFromNick ( "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, getRootElement())
end

function onJoin()
	servertalkprivate("Welcome to My Server", source)
end

addEventHandler("onPlayerJoin",getRootElement(),onJoin)

See Also