From Multi Theft Auto: Wiki
Jump to navigation Jump to search

This event is triggered when a player joins the server. This event is not cancellable.


No parameters.


The source of this event is the player who joined.


This example gets the joined client's name and sends him a welcome message including his name.

-- we register greetPlayer as a handler for the event
function greetPlayer ( )
	-- we store the player's name
	local joinedPlayerName = getPlayerName ( source )
	local serverName = getServerName( )
	-- and send him a greeting
	outputChatBox ( "Welcome " .. joinedPlayerName .. " to ".. serverName .."!" , source, 255, 255, 255 )
addEventHandler ( "onPlayerJoin", root, greetPlayer )

This example sets random color to every player who joins.

local chatColors = {} -- Create a table to save the color

function onPlayerJoin()
	-- Create a table to add rgb values, index will be the player element

	chatColors[source] = {math.random(50, 255), math.random (50, 255), math.random (50, 255)}
addEventHandler("onPlayerJoin", root, onPlayerJoin)

function onPlayerChat(playerMessage, messageType)
	local normalMessage = messageType == 0

	if not normalMessage then
		return false -- If it's not normal type message, cancel code execution

	local playerName = getPlayerName(source) -- Get player name
	local playerColors = chatColors[source] -- Get player stored colors
	local colorR, colorG, colorB = playerColors[1], playerColors[2], playerColors[3] -- Store them into different variables

	outputChatBox(playerName..": #E0D0B0"..playerMessage, root, colorR, colorG, colorB, true) -- Output our message
addEventHandler("onPlayerChat", root, onPlayerChat)

function onPlayerQuit()
	chatColors[source] = nil -- Clear stored data to avoid memory leaks
addEventHandler("onPlayerQuit", root, onPlayerQuit)
Click to collapse [-]
Example 1

This example redirects any player joins to the server automatically to any other server, add the script for admin group.

function Redirect()
redirectPlayer (source, "server ip", "server port")
end    --End function
addEventHandler("onPlayerJoin", root, Redirect)

See Also

Player events

Event functions