SetPlayerName: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(remove extra '</section>' showing in page)
 
(14 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{Server function}}  
{{Server function}}  
__NOTOC__  
__NOTOC__  
This function changes the specified [[player]]'s name.
This function changes the specified [[player]]'s name. Note that any change made to a players name with this function is not saved in their settings so the name change only lasts till they disconnect.


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool setPlayerName ( player thePlayer, string newName )
bool setPlayerName ( player thePlayer, string newName )
</syntaxhighlight>  
</syntaxhighlight>
 
{{OOP||[[player]]:setName|name|getPlayerName}}


===Required Arguments===  
===Required Arguments===  
Line 14: Line 16:
===Returns===
===Returns===
Returns ''true'' if the player name was changed succesfully, ''false'' if invalid arguments are specified.
Returns ''true'' if the player name was changed succesfully, ''false'' if invalid arguments are specified.
===Limits===
* Only ASCII characters between 33 and 126 are allowed (basic latin, no spaces):
<nowiki>!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</nowiki>
* Minimal player name length is 1 character.
* Maximum player name length is 22 characters.
* Player names are case-insensitive. It is not possible to have two clients with same name but different character case.


==Example==
==Example==
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
This example adds a tag before a player's nickname via a /changetag command
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Add an example
 
-- Define the function for this command (/changetag, as defined below)
-- source = the player that triggered this command
-- command = the command passed into the function (changetag)
-- thePlayer = the player that you wish to add a tag to
-- tag = the tag to add to the players nickname
function tagPlayer ( source, command, thePlayer, tag )
-- Attempt to grab the element id for the player from the parsed name.
local sPlayerElement = getPlayerFromName ( thePlayer )
-- Check to see if the player were changing the tag for exists.
if ( sPlayerElement ) then
-- make sure that the element type of thePlayer is acctually pointing to a player element
if getElementType ( sPlayerElement ) == "player" then
-- we store the player's current name,
local oldName = getPlayerName ( sPlayerElement )
-- append the tag passed to this function before it
local taggedName = tag .. oldName
-- then set it as his new name
setPlayerName ( sPlayerElement, taggedName )
-- Tell the player who triggerd the command that the tag has been applied
outputChatBox ( "Player " .. thePlayer .. "'s tag changed to " .. taggedName, source )
end
else
-- Tell the player who triggerd the command that the player could not be found
outputChatBox ( "Unable to change player tag: Player " .. thePlayer .. " not found", source )
end
end
-- Add a command handler for either the console or / chat commands
-- Example: /changetag <playername> <tag>
addCommandHandler ( "changetag", tagPlayer )
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
This example allows you to check if string is valid mta nickname
<syntaxhighlight lang="lua">
function isNicknameValid(nick)
  for _, codepoint in utf8.next, nick do
    if(codepoint < 33 or codepoint > 126)then
      return false
    end
  end
  return true
end
</syntaxhighlight>


==See Also==
==See Also==
{{Player functions}}
{{Player functions}}
[[ru:setPlayerName]]

Latest revision as of 20:27, 6 August 2019

This function changes the specified player's name. Note that any change made to a players name with this function is not saved in their settings so the name change only lasts till they disconnect.

Syntax

bool setPlayerName ( player thePlayer, string newName )


OOP Syntax Help! I don't understand this!

Method: player:setName(...)
Variable: .name
Counterpart: getPlayerName


Required Arguments

  • thePlayer: the player that will have its name set.
  • newName: the new name to set for the player.

Returns

Returns true if the player name was changed succesfully, false if invalid arguments are specified.

Limits

  • Only ASCII characters between 33 and 126 are allowed (basic latin, no spaces):
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
  • Minimal player name length is 1 character.
  • Maximum player name length is 22 characters.
  • Player names are case-insensitive. It is not possible to have two clients with same name but different character case.

Example

Click to collapse [-]
Server

This example adds a tag before a player's nickname via a /changetag command


-- Define the function for this command (/changetag, as defined below)
-- source = the player that triggered this command
-- command = the command passed into the function (changetag)
-- thePlayer = the player that you wish to add a tag to
-- tag = the tag to add to the players nickname
function tagPlayer ( source, command, thePlayer, tag )
	-- Attempt to grab the element id for the player from the parsed name.
	local sPlayerElement = getPlayerFromName ( thePlayer )
	-- Check to see if the player were changing the tag for exists.
	if ( sPlayerElement ) then
		-- make sure that the element type of thePlayer is acctually pointing to a player element
		if getElementType ( sPlayerElement ) == "player" then
			-- we store the player's current name,
			local oldName = getPlayerName ( sPlayerElement )
			-- append the tag passed to this function before it
			local taggedName = tag .. oldName
			-- then set it as his new name
			setPlayerName ( sPlayerElement, taggedName )
			-- Tell the player who triggerd the command that the tag has been applied
			outputChatBox ( "Player " .. thePlayer .. "'s tag changed to " .. taggedName, source )
		end
	else
		-- Tell the player who triggerd the command that the player could not be found
		outputChatBox ( "Unable to change player tag: Player " .. thePlayer .. " not found", source )
	end
end
-- Add a command handler for either the console or / chat commands
-- Example: /changetag <playername> <tag>
addCommandHandler ( "changetag", tagPlayer )

This example allows you to check if string is valid mta nickname

function isNicknameValid(nick)
  for _, codepoint in utf8.next, nick do
    if(codepoint < 33 or codepoint > 126)then
      return false
    end
  end
  return true
end

See Also