Difference between revisions of "GetPlayerFromAccountName"

From Multi Theft Auto: Wiki
(Code)
(Code)
Line 15: Line 15:
 
==Code==
 
==Code==
 
<syntaxhighlight lang="lua">function getPlayerFromAccountName(accountname)
 
<syntaxhighlight lang="lua">function getPlayerFromAccountName(accountname)
if accountname then
+
  if accountname then
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
if getAccountName(getPlayerAccount(player)) == getAccountName(account) then
+
              if getAccountName(getPlayerAccount(player)) == getAccountName(account) then
return player
+
                  return player
end
+
              end
end
+
            end
end
+
        end
end
+
      end
end
+
  end
return false
+
return false
end
 
 
end</syntaxhighlight>
 
end</syntaxhighlight>
  

Revision as of 01:27, 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
               if getAccountName(getPlayerAccount(player)) == getAccountName(account) then
                  return player
               end
            end
         end
      end
   end
return false
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.