GetPlayerFromAccountName: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
Line 18: Line 18:
       for _,account in ipairs(getAccounts()) do
       for _,account in ipairs(getAccounts()) do
         for _,player in ipairs(getElementsByType('player')) do
         for _,player in ipairs(getElementsByType('player')) do
             if getAccountName(account) == accountname then
             if getAccountName(account) == accountname then --Checking that the account exists
               if getAccountName(getPlayerAccount(player)) == getAccountName(account) then
               if getAccountName(getPlayerAccount(player)) == getAccountName(account) then --Checking that the name of the account matches that of a player
                   return player
                   return player
               end
               end

Revision as of 01:35, 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(accountname)
   if accountname then
      for _,account in ipairs(getAccounts()) do
         for _,player in ipairs(getElementsByType('player')) do
            if getAccountName(account) == accountname then --Checking that the account exists
               if getAccountName(getPlayerAccount(player)) == getAccountName(account) then --Checking that the name of the account matches that of a player
                  return player
               end
            end
         end
      end
   end
   return false
end

Example

This example get player name from account name by command

addCommandHandler ("name",
function (player,_,accountname)
    if accountname then
        local thePlayer = getPlayerFromAccountName (accountname)
        if thePlayer then
            outputChatBox (getPlayerName (thePlayer),player,255,255,255,true)
        else
        outputChatBox ("Player not found",player,255,0,0)
        end
    else
    outputChatBox ("Write the name of the account first",player,255,255,0)
    end
end
)

Author: Motar2k~