GetPlayerSerial: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server client function}}
{{Shared function}}
This function returns the [[serial]] for a specified [[player]].
This function returns the [[serial]] for a specified [[player]].
{{Note|client side getPlayerSerial() can occasionally return the wrong value.}}
{{Note|It's possible for a player to fake the value returned from getPlayerSerial when used on the clientside. For this reason you should only trust the value returned by this function when used serverside. }}
{{Important Note|You should '''use this function''' in conjunction '''with account system''' (e.g: '''login & password''') - especially for critical things, because '''serials could be invalid''' (as in, '''non-unique''' or '''faked'''). See: [https://wiki.multitheftauto.com/wiki/Script_security Script security].}}


==Syntax==
==Syntax==
Line 12: Line 13:
===Required Arguments===
===Required Arguments===
*'''thePlayer:''' A [[player]] object referencing the specified player.
*'''thePlayer:''' A [[player]] object referencing the specified player.
==Returns==
Returns the serial as a ''string'' if it was found, ''false'' otherwise.
</section>
</section>
==Syntax==
<section name="Client" class="client" show="true">
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
string getPlayerSerial ( )
string getPlayerSerial ( )
</syntaxhighlight>
</syntaxhighlight>
{{OOP||[[Player]].getSerial}}
{{OOP||[[localPlayer]]:getSerial||}}
</section>
===Required Arguments===
None


==Returns==
==Returns==
Returns the serial as a ''string'' if it was found, ''false'' otherwise.
Returns the serial as a ''string'' if it was found, ''false'' otherwise.
</section>


==Example==
==Serverside examples==
<section name="Example 1" class="server" show="true">
<section name="Server" class="server" show="true">
This example creates a command with which player can check their own serial.
This example creates a command with which player can check their own serial.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 37: Line 45:
addCommandHandler( "myserial", checkMySerial )
addCommandHandler( "myserial", checkMySerial )
</syntaxhighlight>
</syntaxhighlight>
</section>


<section name="Example 2" class="server" show="true">
This example adds a command to ban a player's serial.
This example add command to ban player serial.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function banSerial( source, command, noob, reason )
local function banSerialCommand ( source, command, playerName, reason )
   if ( noob ) then
   if playerName then
       local theNoob = getPlayerFromName( noob )
       local player, serial = getPlayerFromName ( playerName ), getPlayerSerial ( playerName )
      local theNoobSerial = getPlayerSerial( theNoob )
       if player then
       if ( theNoob ) then
         addBan ( serial, source, reason )
         addBan( theNoobSerial, source, reason )
       end
       end
   end
   end
end
end
addCommandHandler( "banserial", banSerial )
addCommandHandler ( "banplayerserial", banSerialCommand )
</syntaxhighlight>
</syntaxhighlight>
</section>


<section name="Example 3" class="client" show="true">
This example only allows clients with a certain serial to log in into an account.
This example add command to get client's serial.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function getMySerial( )
local allowedAccountSerials =  
    local theSerial = getPlayerSerial()
    outputChatBox("Your serial is: "..tostring(theSerial))
end
addCommandHandler( "myserial", getMySerial )
</syntaxhighlight>
</section>
 
<section name="Example 4" class="server" show="true">
This example is Firewall Account Player by serial on Login
<syntaxhighlight lang="lua">
Firewall =  
{
{
     [ 'AccountName' ] = 'SerialPlayer',
     -- List of allowed serials to log in into an account. Format:
     [ '3ash8' ] = '9C9F3B55D9D7BB7135FF274D3BF444E4',
    -- [ Account name ] = { Allowed serial array }
     [ 'test5' ] = '1D6F76CF8D7193792D13789849498452',
     [ "3ash8" ] = { "9C9F3B55D9D7BB7135FF274D3BF444E4" },
     [ "test5" ] = { "1D6F76CF8D7193792D13789849498452" },
}
}
   
   
addEventHandler ( 'onPlayerLogin', getRootElement ( ),
addEventHandler("onPlayerLogin", root,
     function ( _, theCurrentAccount )
     function(_, account)
    local Serial = Firewall[getAccountName(theCurrentAccount)]
        -- Get the player serial and the allowed serial list for that account
         if ( Serial ) then
        -- (If no serial is allowed for the account, do not allow the player to log in as a safety measure)
             if Serial ~= getPlayerSerial ( source ) then
        local playerSerial, allowedSerials = getPlayerSerial(source), allowedAccountSerials[getAccountName(account)] or {}
                 banPlayer ( source, false, false, true, getRootElement ( ), 'reason ban' )
         -- Check whether the client has an allowed serial or not
        for i = 1, #allowedSerials do
             if allowedSerials[i] == playerSerial then
                 -- The serial is allowed. Proceed with the normal log in proccess
                return
             end
             end
         end
         end
        -- If we reach this point the serial is not allowed. Do not let the player log in
        cancelEvent(true, "Client serial not allowed to log in into the account")
     end
     end
)
)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
==See Also==
{{Player functions}}


==See Also==
[[ar:getPlayerSerial]]
{{Serial functions}}
[[en:GetPlayerSerial]]

Latest revision as of 16:15, 2 February 2024

This function returns the serial for a specified player.

[[{{{image}}}|link=|]] Note: It's possible for a player to fake the value returned from getPlayerSerial when used on the clientside. For this reason you should only trust the value returned by this function when used serverside.
[[{{{image}}}|link=|]] Important Note: You should use this function in conjunction with account system (e.g: login & password) - especially for critical things, because serials could be invalid (as in, non-unique or faked). See: Script security.

Syntax

Click to collapse [-]
Server
string getPlayerSerial ( player thePlayer )

OOP Syntax Help! I don't understand this!

Method: player:getSerial(...)
Variable: .serial


Required Arguments

  • thePlayer: A player object referencing the specified player.

Returns

Returns the serial as a string if it was found, false otherwise.

Syntax

Click to collapse [-]
Client
string getPlayerSerial ( )

OOP Syntax Help! I don't understand this!

Method: localPlayer:getSerial(...)


Required Arguments

None

Returns

Returns the serial as a string if it was found, false otherwise.

Serverside examples

Click to collapse [-]
Server

This example creates a command with which player can check their own serial.

function checkMySerial( thePlayer, command )
    local theSerial = getPlayerSerial( thePlayer )
    if theSerial then
        outputChatBox( "Your serial is: " .. theSerial, thePlayer )
    else
        outputChatBox( "Sorry, you have no serial. =(", thePlayer )
    end
end
addCommandHandler( "myserial", checkMySerial )

This example adds a command to ban a player's serial.

local function banSerialCommand ( source, command, playerName, reason )
   if playerName then
      local player, serial = getPlayerFromName ( playerName ), getPlayerSerial ( playerName )
      if player then
         addBan ( serial, source, reason )
      end
   end
end
addCommandHandler ( "banplayerserial", banSerialCommand )

This example only allows clients with a certain serial to log in into an account.

local allowedAccountSerials = 
{
    -- List of allowed serials to log in into an account. Format:
    -- [ Account name ] = { Allowed serial array }
    [ "3ash8" ] = { "9C9F3B55D9D7BB7135FF274D3BF444E4" },
    [ "test5" ] = { "1D6F76CF8D7193792D13789849498452" },
}
 
addEventHandler("onPlayerLogin", root,
    function(_, account)
        -- Get the player serial and the allowed serial list for that account
        -- (If no serial is allowed for the account, do not allow the player to log in as a safety measure)
        local playerSerial, allowedSerials = getPlayerSerial(source), allowedAccountSerials[getAccountName(account)] or {}
        -- Check whether the client has an allowed serial or not
        for i = 1, #allowedSerials do
            if allowedSerials[i] == playerSerial then
                -- The serial is allowed. Proceed with the normal log in proccess
                return
            end
        end
        -- If we reach this point the serial is not allowed. Do not let the player log in
        cancelEvent(true, "Client serial not allowed to log in into the account")
    end
)

See Also