OnPlayerConnect: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
Megadreams (talk | contribs) (Remove unnecessary comment about "if-else hell") |
||
(One intermediate revision by one other user not shown) | |||
Line 28: | Line 28: | ||
==Example== | ==Example== | ||
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. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
--when a player connects | --when a player connects | ||
function playerConnect(playerNick, playerIP, playerUsername, playerSerial) | function playerConnect(playerNick, playerIP, playerUsername, playerSerial) | ||
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.') | |||
return | |||
end | |||
-- 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 | --add the playerConnect function as a handler for onPlayerConnect | ||
addEventHandler("onPlayerConnect", | addEventHandler("onPlayerConnect", root, playerConnect) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 50: | Line 48: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function versionCheck(_,_,_,_, clientVersion) | function versionCheck(_,_,_,_, clientVersion) | ||
if clientVersion < 259 then | |||
cancelEvent(true, "Update your MTA before you join this server!") | |||
end | |||
end | end | ||
addEventHandler("onPlayerConnect", | addEventHandler("onPlayerConnect", root, versionCheck) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 21:29, 3 June 2024
This event is triggered when a player attempts to connect to the server.
WARNING:
|
Parameters
string playerNick, string playerIP, string playerUsername, string playerSerial, int playerVersionNumber, string playerVersionString
- playerNick: The player's current nickname.
- playerIP: The player's current IP.
- playerUsername: The player's community username.
- playerSerial: The player's serial number.
- playerVersionNumber: The player's MTA version in pure numerical form, e.g. '256' for 1.0, '257' for 1.0.1, etc.
- playerVersionString: The player's MTA version in sortable string form. Same as the return value from getPlayerVersion.
Source
The source of this event is the client's root element.
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
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) 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.') return end -- output some data about the player outputChatBox(playerNick.." just connected to the server.") outputChatBox("IP: "..playerIP.." / Username: "..playerUsername.." / Serial: "..playerSerial) end --add the playerConnect function as a handler for onPlayerConnect addEventHandler("onPlayerConnect", root, playerConnect)
This example cancels connection if player uses older MTA (older than 1.0.3)
function versionCheck(_,_,_,_, clientVersion) if clientVersion < 259 then cancelEvent(true, "Update your MTA before you join this server!") end end addEventHandler("onPlayerConnect", root, versionCheck)
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