GetPlayerFromAccountName: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Reverted edits by AncienT (talk) to last revision by XNawaf)
Line 14: Line 14:


==Code==
==Code==
<syntaxhighlight lang="lua">function getPlayerFromAccountName(accountname)
<syntaxhighlight lang="lua">function getPlayerFromAccountName(name)  
  if accountname then
    local acc = getAccount (name)
      for _,account in ipairs(getAccounts()) do
    if name and acc and not isGuestAccount (acc) then
        for _,player in ipairs(getElementsByType('player')) do
        return getAccountPlayer (acc)
            if getAccountName(account) == accountname then --Checking that the account exists
    else
              if getAccountName(getPlayerAccount(player)) == getAccountName(account) then --Checking that the name of the account matches that of a player
        return false
                  return player
    end
              end
            end
        end
      end
  end
  return false
end</syntaxhighlight>
end</syntaxhighlight>


Line 32: Line 26:
This example get player name from account name by command
This example get player name from account name by command
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addCommandHandler ("name",
addCommandHandler ("getPlayerName",
function (player,_,accountname)
    function (player,_,name)
    if accountname then
        if name then
         local thePlayer = getPlayerFromAccountName (accountname)
         local thePlayer = getPlayerFromAccountName (name)
         if thePlayer then
         if thePlayer then
             outputChatBox (getPlayerName (thePlayer),player,255,255,255,true)
             outputChatBox (getPlayerName (thePlayer),player,255,255,255,true)
         else
         else
        outputChatBox ("Player not found",player,255,0,0)
            outputChatBox ("player not found !",player,255,0,0)
         end
         end
     else
     else
    outputChatBox ("Write the name of the account first",player,255,255,0)
        outputChatBox ("type account name frist !",player,255,255,0)
     end
     end
end
end
Line 48: Line 42:
</syntaxhighlight>
</syntaxhighlight>


'''Author: Motar2k~'''
'''Author: ,#Na[W]aF'1000.'''

Revision as of 08:14, 15 January 2019


This function get online player from their account name.

Syntax

player getPlayerFromAccountName ( string accountName )

Required Arguments

  • accountName: A string containing account name of a player you want to reference.

Returns

Returns a player if the account name exists,false if player not found.

Code

function getPlayerFromAccountName(name) 
    local acc = getAccount (name)
    if name and acc and not isGuestAccount (acc) then
        return getAccountPlayer (acc)
    else
        return false
    end
end

Example

This example get player name from account name by command

addCommandHandler ("getPlayerName",
    function (player,_,name)
        if name then
        local thePlayer = getPlayerFromAccountName (name)
        if thePlayer then
            outputChatBox (getPlayerName (thePlayer),player,255,255,255,true)
        else
            outputChatBox ("player not found !",player,255,0,0)
        end
    else
        outputChatBox ("type account name frist !",player,255,255,0)
    end
end
)

Author: ,#Na[W]aF'1000.