Difference between revisions of "SetAccountName"

From Multi Theft Auto: Wiki
(Added to MTA natively in https://buildinfo.mtasa.com/index.php?Revision=11747)
Line 1: Line 1:
{{Useful Function}}
+
{{Server function}}
{{Delete|Added to MTA natively in r11747.}}
 
 
__NOTOC__
 
__NOTOC__
This function is used to change existing [[account]]s name.<br>
+
This function sets the name of an [[account]].
  
==Syntax==
+
==Syntax==  
<syntaxhighlight lang="lua">bool setAccountName ( element player, string oldAccount, string newAccount )</syntaxhighlight>
+
<syntaxhighlight lang="lua">
 +
bool setAccountName ( account theAccount, string name [, bool allowCaseVariations = false] )
 +
</syntaxhighlight>  
 +
{{OOP||[[account]]:setName||name}}
 +
===Required Arguments===
 +
*'''theAccount:''' The account you wish to change the name.
 +
*'''name:''' The new name.
  
===Required Arguments===
+
===Optional Arguments===
* '''player''': The player who called the function.
+
*'''allowCaseVariations:''' Whether the username is case sensitive (if this is set to true, usernames "Bob" and "bob" will refer to different accounts)
* '''oldAccount''': The account you want to change.
 
* '''newAccount''': The name you wanna to apply old account on it.
 
  
 
===Returns===
 
===Returns===
Returns ''true'' if account was successfully changed, ''false'' if the old account does not exist/ the new account exist.
+
Returns a ''true'' if the account name was set, ''false'' if an invalid argument was specified.
  
==Code==
+
==Example==  
<section name="Server" class="server" show="true">
+
Change the name of an account.
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
setAccountName = function ( plr, old, new )
+
addCommandHandler("changeaccountname", function(player, _, oldname, newname)
if old and new then
+
    if not oldname or not newname then
local oldAccount, newAccount, newPass = getAccount ( old ), getAccount ( new ), "mta"..math.random(10000,100000)
+
        return
if oldAccount and not newAccount then
+
    end
if addAccount ( new, newPass ) then
+
    local account = getAccountByName(oldname)
local newAccount = getAccount ( new )
+
    if not account then
local player = getAccountPlayer ( oldAccount )
+
        return
for index, value in pairs ( getAllAccountData ( oldAccount ) ) do
+
    end
setAccountData ( newAccount, index, value )
+
    setAccountName(account, newname)
end
+
end)
for index, value in ipairs ( aclGroupList ( ) ) do
 
if isObjectInACLGroup ( "user."..old, value ) then
 
aclGroupAddObject ( value, "user."..new )
 
aclGroupRemoveObject ( value, "user."..old )
 
end
 
end
 
if player then
 
logOut ( player )
 
logIn ( player, newAccount, newPass )
 
if plr == player then
 
outputChatBox ( "* Your new account and password: [ "..new.." ] [ "..newPass.." ].", player, 255, 255, 0, true )
 
else
 
outputChatBox ( "* Your new account and password: [ "..new.." ] [ "..newPass.." ].", player, 255, 255, 0, true )
 
outputChatBox ( "* New account and password: [ "..new.." ] [ "..newPass.." ].", plr, 255, 255, 0, true )
 
end
 
else
 
outputChatBox ( "* New account and password: [ "..new.." ] [ "..newPass.." ].", plr, 255, 255, 0, true )
 
end
 
setTimer ( removeAccount, 100, 1, oldAccount )
 
return true
 
end
 
end
 
end
 
return false
 
end
 
 
</syntaxhighlight>
 
</syntaxhighlight>
</section>
 
 
==Example==
 
<section name="Server" class="server" show="true">
 
This example will change account name.
 
<syntaxhighlight lang="lua">addCommandHandler ( "chgAccName", function ( player, _, old, new )
 
setAccountName ( player, old, new )
 
end )</syntaxhighlight></section>
 
  
==Author==
+
==See Also==
Created By 3NAD.
+
{{Account_functions}}

Revision as of 22:26, 1 July 2018

This function sets the name of an account.

Syntax

bool setAccountName ( account theAccount, string name [, bool allowCaseVariations = false] )

OOP Syntax Help! I don't understand this!

Method: account:setName(...)
Counterpart: name

Required Arguments

  • theAccount: The account you wish to change the name.
  • name: The new name.

Optional Arguments

  • allowCaseVariations: Whether the username is case sensitive (if this is set to true, usernames "Bob" and "bob" will refer to different accounts)

Returns

Returns a true if the account name was set, false if an invalid argument was specified.

Example

Change the name of an account.

addCommandHandler("changeaccountname", function(player, _, oldname, newname)
    if not oldname or not newname then
        return
    end
    local account = getAccountByName(oldname)
    if not account then
        return 
    end
    setAccountName(account, newname)
end)

See Also