SetAccountData: Difference between revisions
No edit summary |
Zangomangu (talk | contribs) (Removed fixed issue) |
||
(32 intermediate revisions by 25 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | {{Server function}} | ||
{{ | __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.}} | ||
This function sets a string to be stored in an account. This can then be retrieved using [[ | This function sets a string to be stored in an [[account]]. This can then be retrieved using [[getAccountData]]. Data stored as account data is persistent across user's sessions and maps, unless they are logged into a guest account. Even if logged into a guest account, account data can be useful as a way to store a reference to your own account system, though it's persistence is equivalent to that of using [[setElementData]] on the player's element. | ||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
bool setAccountData ( account theAccount, string key, | bool setAccountData ( account theAccount, string key, var value ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{OOP||[[account]]:setData||getAccountData}} | |||
===Required Arguments=== | ===Required Arguments=== | ||
*'''theAccount:''' The account you wish to retrieve the data from. | *'''theAccount:''' The account you wish to retrieve the data from. | ||
*'''key:''' The key under which you wish to store the data | *'''key:''' The key under which you wish to store the data | ||
*'''value:''' The value you wish to store | *'''value:''' The value you wish to store. Set to false to remove the data. '''NOTE:''' you cannot store tables as values, but you can use [[toJSON]] strings. | ||
===Returns=== | ===Returns=== | ||
Line 17: | Line 17: | ||
==Example== | ==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. | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function onPlayerQuit ( ) | |||
-- when a player leaves, store his current money amount in his account data | |||
local playeraccount = getPlayerAccount ( source ) | |||
if ( playeraccount ) and not isGuestAccount ( playeraccount ) then -- if the player is logged in | |||
local playermoney = getPlayerMoney ( source ) -- get the player money | |||
setAccountData ( playeraccount, "piraterpg.money", playermoney ) -- save it in his account | |||
end | |||
end | |||
function onPlayerLogin (_, playeraccount ) | |||
-- when a player logins, retrieve his money amount from his account data and set it | |||
if ( playeraccount ) then | |||
local playermoney = getAccountData ( playeraccount, "piraterpg.money" ) | |||
-- 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 | |||
end | |||
addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) | |||
addEventHandler ( "onPlayerLogin", root, onPlayerLogin ) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
==See Also== | ==See Also== | ||
{{Account_functions}} | {{Account_functions}} | ||
[[ | |||
[[en:setAccountData]] | |||
[[ru:setAccountData]] | |||
[[ar:setAccountData]] | |||
[[es:setAccountData]] |
Latest revision as of 10:41, 30 January 2022
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 sets a string to be stored in an account. This can then be retrieved using getAccountData. Data stored as account data is persistent across user's sessions and maps, unless they are logged into a guest account. Even if logged into a guest account, account data can be useful as a way to store a reference to your own account system, though it's persistence is equivalent to that of using setElementData on the player's element.
Syntax
bool setAccountData ( account theAccount, string key, var value )
OOP Syntax Help! I don't understand this!
- Method: account:setData(...)
- Counterpart: getAccountData
Required Arguments
- theAccount: The account you wish to retrieve the data from.
- key: The key under which you wish to store the data
- value: The value you wish to store. Set to false to remove the data. NOTE: you cannot store tables as values, but you can use toJSON strings.
Returns
Returns a true if the account data was set, false if an invalid argument was specified.
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 ( ) -- when a player leaves, store his current money amount in his account data local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) and not isGuestAccount ( playeraccount ) then -- if the player is logged in local playermoney = getPlayerMoney ( source ) -- get the player money setAccountData ( playeraccount, "piraterpg.money", playermoney ) -- save it in his account end end function onPlayerLogin (_, playeraccount ) -- when a player logins, retrieve his money amount from his account data and set it if ( playeraccount ) then local playermoney = getAccountData ( playeraccount, "piraterpg.money" ) -- 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 end addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) addEventHandler ( "onPlayerLogin", root, onPlayerLogin )
See Also
- addAccount
- copyAccountData
- getAccount
- getAccountData
- getAccountName
- getAccountPlayer
- getAccountSerial
- getAccounts
- getAccountsBySerial
- getAllAccountData
- getPlayerAccount
- isGuestAccount
- logIn
- logOut
- removeAccount
- setAccountData
- setAccountPassword
- getAccountByID
- getAccountID
- getAccountIP
- getAccountsByData
- getAccountsByIP
- setAccountName