GetAccountData: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
m (Edited language change)
Tag: Manual revert
 
(17 intermediate revisions by 14 users not shown)
Line 1: Line 1:
{{Server function}}
{{Server function}}
__NOTOC__
__NOTOC__{{Important Note|You '''MUST''' use the standard ''module.key'' naming for your keys, as shown in the example below. This prevents collisions between different scripts.}}
{{Note_box|It is strongly recommended that you use the standard ''module.key'' naming for your keys, as shown in the example below. This prevents collisions between different scripts.}}
This function retrieves a string that has been stored using [[setAccountData]]. Data stored as account data is persistent across user's sessions and maps, unless they are logged into a guest account.
This function retrieves a string that has been stored using [[setAccountData]]. Data stored as account data is persistent across user's sessions and maps, unless they are logged into a guest account.


Line 8: Line 7:
string getAccountData ( account theAccount, string key )
string getAccountData ( account theAccount, string key )
</syntaxhighlight>  
</syntaxhighlight>  
 
{{OOP||[[account]]:getData||setAccountData}}
===Required Arguments===  
===Required Arguments===  
*'''theAccount:''' The account you wish to retrieve the data from.
*'''theAccount:''' The account you wish to retrieve the data from.
Line 19: Line 18:
For a pirate roleplaying gametype, the amount of money a player has is made persistent by storing it in his account. Note the code uses "piraterpg.money" as key instead of just "money", as the player may be participating in other gametypes that also save his money amount to his account. If both gametypes would use "money" as the account key, they'd overwrite each other's data.
For a pirate roleplaying gametype, the amount of money a player has is made persistent by storing it in his account. Note the code uses "piraterpg.money" as key instead of just "money", as the player may be participating in other gametypes that also save his money amount to his account. If both gametypes would use "money" as the account key, they'd overwrite each other's data.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function onPlayerQuit ( )
function onPlayerQuit()
      -- when a player leaves, store his current money amount in his account data
       local playerAccount = getPlayerAccount(source) -- get his account
       local playeraccount = getClientAccount ( source )
       if (playerAccount) then -- if we got the account then
       if ( playeraccount ) then
             local playerMoney = getPlayerMoney(source) -- get his money amount
             local playermoney = getPlayerMoney ( source )
             setAccountData(playerAccount, "piraterpg.money", playerMoney) -- store his current money amount in his account data
             setAccountData ( playeraccount, "piraterpg.money", playermoney )
       end
       end
end
end
addEventHandler("onPlayerQuit", root, onPlayerQuit) -- add an event handler


function onPlayerJoin ( )
function onPlayerLogin(_,account)
      -- when a player joins, retrieve his money amount from his account data and set it
    local playerMoney = getAccountData(account, "piraterpg.money") -- get the money amount was store in his account data
      local playeraccount = getClientAccount ( source )
    -- make sure there was actually a value saved under this key (check if playerMoney is not false).
      if ( playeraccount ) then
    -- this will for example not be the case when a player plays the gametype for the first time
            local playermoney = getAccountData ( playeraccount, "piraterpg.money" )
    if (playerMoney) then
            -- make sure there was actually a value saved under this key (check if playermoney is not false).
        setPlayerMoney(source, playerMoney)
            -- this will for example not be the case when a player plays the gametype for the first time
    end
            if ( playermoney ) then
                  setPlayerMoney ( source, playermoney )
            end
      end
end
end
 
addEventHandler("onPlayerLogin", root, onPlayerLogin) -- add an event handler
addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit )
addEventHandler ( "onPlayerJoin", getRootElement ( ), onPlayerJoin )
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Account_functions}}
{{Account_functions}}
[[ru:getAccountData]]
[[ar:getAccountData]]
[[pl:getAccountData]]
[[zh-cn:getAccountData]]

Latest revision as of 15:12, 8 June 2022

[[{{{image}}}|link=|]] Important Note: You MUST use the standard module.key naming for your keys, as shown in the example below. This prevents collisions between different scripts.

This function retrieves a string that has been stored using setAccountData. Data stored as account data is persistent across user's sessions and maps, unless they are logged into a guest account.

Syntax

string getAccountData ( account theAccount, string key )

OOP Syntax Help! I don't understand this!

Method: account:getData(...)
Counterpart: setAccountData


Required Arguments

  • theAccount: The account you wish to retrieve the data from.
  • key: The key under which the data is stored

Returns

Returns a string containing the stored data or false if no data was stored under that key.

Example

For a pirate roleplaying gametype, the amount of money a player has is made persistent by storing it in his account. Note the code uses "piraterpg.money" as key instead of just "money", as the player may be participating in other gametypes that also save his money amount to his account. If both gametypes would use "money" as the account key, they'd overwrite each other's data.

function onPlayerQuit()
      local playerAccount = getPlayerAccount(source) -- get his account
      if (playerAccount) then -- if we got the account then
            local playerMoney = getPlayerMoney(source) -- get his money amount
            setAccountData(playerAccount, "piraterpg.money", playerMoney) -- store his current money amount in his account data
      end
end
addEventHandler("onPlayerQuit", root, onPlayerQuit) -- add an event handler

function onPlayerLogin(_,account)
    local playerMoney = getAccountData(account, "piraterpg.money") -- get the money amount was store in his account data
    -- make sure there was actually a value saved under this key (check if playerMoney is not false).
    -- this will for example not be the case when a player plays the gametype for the first time
    if (playerMoney) then
        setPlayerMoney(source, playerMoney)
    end
end
addEventHandler("onPlayerLogin", root, onPlayerLogin) -- add an event handler

See Also