OnPlayerChangeNick: Difference between revisions

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


==Example==  
==Example==  
<section name="Server Example 1" class="server" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function nickChangeHandler(oldNick, newNick)
function nickChangeHandler(oldNick, newNick)
Line 32: Line 33:
-- add an event handler
-- add an event handler
addEventHandler("onPlayerChangeNick", getRootElement(), nickChangeHandler)
addEventHandler("onPlayerChangeNick", getRootElement(), nickChangeHandler)
</syntaxhighlight>
{{New items|3.0160|1.5.3|</syntaxhighlight></section>
 
This function checks if the nickname of a player was changed by a script, if so, a message is shown in the server logs.
This example displays a message to everyone every time player changed his nick
<section name="Server Example 2" class="server" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function nickChangeHandler(oldNick, newNick)
function wasNickChangedByUser(oldNick, newNick, changedByUser)
outputChatBox(oldNick.." is now known as "..newNick, getRootElement(), 255, 100, 100) -- display the message
if (changedByUser == false) then -- check if the nickname was not changed by the user
outputServerLog("Player "..oldNick.."'s name was changed to "..newNick.." by a script") -- output to the server's logs
end
end
end
addEventHandler("onPlayerChangeNick", getRootElement(), nickChangeHandler) -- add an event handler
addEventHandler("onPlayerChangeNick", getRootElement(), wasNickChangedByUser) -- add an event handler
</syntaxhighlight>
</syntaxhighlight></section>|9765}}


==Issues==
==Issues==

Revision as of 11:41, 22 October 2016

This event is triggered when a player changes his nickname.

Parameters

string oldNick, string newNick, bool changedByUser
  • oldNick: the nickname the player had before.
  • newNick: the new nickname of the player.
  • changedByUser: whether the name was changed using setPlayerName or by the user

Source

The source of this event is the player that changed his nick

Cancel effect

Cancelling this event depends on how it is called, if it is called by the scripting event then it is NOT cancelable. If it is called from the /nick command it IS cancelable. If this event is cancelled and can be cancelled then the name will not change.

Example

Click to collapse [-]
Server Example 1
function nickChangeHandler(oldNick, newNick)
    -- check if there's account with newNick as username
    if getAccount(newNick) then
        outputChatBox("Sorry, there already exists an account with your new nickname as username.", source, 0, 255, 0)
        outputChatBox("Please choose another one.", source, 0, 255, 0)
        -- cancel the event to prevent the nick from being changed
        cancelEvent()
    end
end
-- add an event handler
addEventHandler("onPlayerChangeNick", getRootElement(), nickChangeHandler)
{{New items|3.0160|1.5.3|

This function checks if the nickname of a player was changed by a script, if so, a message is shown in the server logs.

Click to collapse [-]
Server Example 2
function wasNickChangedByUser(oldNick, newNick, changedByUser)
	if (changedByUser == false) then -- check if the nickname was not changed by the user
		outputServerLog("Player "..oldNick.."'s name was changed to "..newNick.." by a script") -- output to the server's logs
	end	
end
addEventHandler("onPlayerChangeNick", getRootElement(), wasNickChangedByUser) -- add an event handler

|9765}}

Issues

Template:GH Issue
Issue ID Description
#7362 onPlayerChangeNick triggered when nick doesn't changed

See Also

Player events


Event functions