Difference between revisions of "OnPlayerConnect"

From Multi Theft Auto: Wiki
(Clarification of playerVersion parameter)
(Example)
Line 19: Line 19:
  
 
==Example==  
 
==Example==  
 +
<section name="Server" class="server" show="true">
 
<!-- Explain what the example is in a single sentance -->
 
<!-- Explain what the example is in a single sentance -->
 
This example cancels connection attempts of people who use the nick "Player" or outputs some data about the connecting player otherwise.
 
This example cancels connection attempts of people who use the nick "Player" or outputs some data about the connecting player otherwise.
Line 37: Line 38:
 
addEventHandler ("onPlayerConnect", getRootElement(), playerConnect)
 
addEventHandler ("onPlayerConnect", getRootElement(), playerConnect)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</section>
  
 +
 +
<section name="Server" class="server" show="true">
 +
This example cancels connection if player uses older MTA (older than 1.0.3)
 +
<syntaxhighlight lang="lua">
 +
addEventHandler( "onPlayerConnect", getRootElement(),
 +
    function ( _,_,_,_, clientVersion )
 +
        if ( clientVersion < 259 ) then
 +
            cancelEvent( true, "Update your MTA before you join this server!" );
 +
        end
 +
    end
 +
)
 +
</syntaxhighlight>
 +
</section>
 
{{See also/Server event|Player events}}
 
{{See also/Server event|Player events}}

Revision as of 12:56, 4 June 2010

This event is triggered when a player attempts to connect to the server.

Parameters

string playerNick, string playerIP, string playerUsername, string playerSerial, int playerVersion
  • playerNick: The player's current nickname.
  • playerIP: The player's current IP.
  • playerUsername: The player's community username.
  • playerSerial: The player's serial number.

Extra parameter from 1.0.2 onwards:

  • playerVersion: The player's MTA version in pure numerical form, e.g. '256' for 1.0, '257' for 1.0.1, etc.

Cancel effect

If this event is canceled, the player will be disconnected with an error message saying the reason specified in cancelEvent or "Disconnected: server refused the connection" if none was specified.

Example

Click to collapse [-]
Server

This example cancels connection attempts of people who use the nick "Player" or outputs some data about the connecting player otherwise.

--when a player connects
function playerConnect (playerNick, playerIP, playerUsername, playerSerial, playerVersion)
    if playerNick == "Player" then --check if his nick is "Player"
        cancelEvent(true,"The nick \"Player\" is not allowed, please change it to something else. You can change your nick in Settings menu Multiplayer tab.") --in that case refuse the connection
    else
        --output some data about the player
        outputChatBox (playerNick.." just connected to the server.")
        outputChatBox ("IP: "..playerIP.." Username: "..playerUsername.." Serial: "..playerSerial)
    end
end

--add the playerConnect function as a handler for onPlayerConnect
addEventHandler ("onPlayerConnect", getRootElement(), playerConnect)


Click to collapse [-]
Server

This example cancels connection if player uses older MTA (older than 1.0.3)

addEventHandler( "onPlayerConnect", getRootElement(),
    function ( _,_,_,_, clientVersion )
        if ( clientVersion < 259 ) then
            cancelEvent( true, "Update your MTA before you join this server!" );
        end
    end
)

See Also

Player events

Event functions