OnPlayerJoin: Difference between revisions
Jump to navigation
Jump to search
(Improve example, fix memory leak.) |
|||
(5 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Server event}} | {{Server event}} | ||
This event is triggered when a player joins the server. | This event is triggered when a player joins the server. This event is not cancellable. | ||
==Parameters== | ==Parameters== | ||
Line 21: | Line 21: | ||
outputChatBox ( "Welcome " .. joinedPlayerName .. " to ".. serverName .."!" , source, 255, 255, 255 ) | outputChatBox ( "Welcome " .. joinedPlayerName .. " to ".. serverName .."!" , source, 255, 255, 255 ) | ||
end | end | ||
addEventHandler ( "onPlayerJoin", | addEventHandler ( "onPlayerJoin", root, greetPlayer ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
This example sets random color to every player who joins. | This example sets random color to every player who joins. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
-- | local chatColors = {} -- Create a table to save the color | ||
function onPlayerJoin() | |||
function | -- 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)} | |||
end | end | ||
addEventHandler("onPlayerJoin", root, onPlayerJoin) | |||
function | |||
function onPlayerChat(playerMessage, messageType) | |||
local normalMessage = messageType == 0 | |||
if not normalMessage then | |||
return false -- If it's not normal type message, cancel code execution | |||
end | end | ||
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 | |||
cancelEvent() | |||
end | |||
addEventHandler("onPlayerChat", root, onPlayerChat) | |||
function onPlayerQuit() | |||
chatColors[source] = nil -- Clear stored data to avoid memory leaks | |||
end | end | ||
addEventHandler ( " | addEventHandler("onPlayerQuit", root, onPlayerQuit) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<section name="Example 1" class="server" show="true"> | <section name="Example 1" class="server" show="true"> | ||
This example | This example redirects any player joins to the server automatically to any other server, add the script for admin group. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function Redirect() | function Redirect() | ||
redirectPlayer (source, "server ip", "server port") | redirectPlayer (source, "server ip", "server port") | ||
end --End function | end --End function | ||
addEventHandler("onPlayerJoin", | addEventHandler("onPlayerJoin", root, Redirect) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | </section> | ||
{{See also/Server event|Player events}} | {{See also/Server event|Player events}} |
Latest revision as of 21:55, 18 February 2022
This event is triggered when a player joins the server. This event is not cancellable.
Parameters
No parameters.
Source
The source of this event is the player who joined.
Example
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 ) end 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)} end 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 end 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 cancelEvent() end addEventHandler("onPlayerChat", root, onPlayerChat) function onPlayerQuit() chatColors[source] = nil -- Clear stored data to avoid memory leaks end addEventHandler("onPlayerQuit", root, onPlayerQuit)
Click to collapse [-]
Example 1This 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
- onPlayerACInfo
- onPlayerBan
- onPlayerChangeNick
- onPlayerChat
- onPlayerClick
- onPlayerCommand
- onPlayerConnect
- onPlayerContact
- onPlayerDamage
- onPlayerJoin
- onPlayerLogin
- onPlayerLogout
- onPlayerMarkerHit
- onPlayerMarkerLeave
- onPlayerModInfo
- onPlayerMute
- onPlayerNetworkStatus
- onPlayerPickupHit
- onPlayerPickupLeave
- onPlayerPickupUse
- onPlayerPrivateMessage
- onPlayerQuit
- onPlayerScreenShot
- onPlayerSpawn
- onPlayerStealthKill
- onPlayerTarget
- onPlayerUnmute
- onPlayerVehicleEnter
- onPlayerVehicleExit
- onPlayerVoiceStart
- onPlayerVoiceStop
- onPlayerWasted
- onPlayerWeaponFire
- onPlayerWeaponSwitch
Event functions
- addEvent
- addEventHandler
- cancelEvent
- cancelLatentEvent
- getEventHandlers
- getLatentEventHandles
- getLatentEventStatus
- removeEventHandler
- triggerEvent
- wasEventCancelled