SetElementData: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 22: Line 22:
This example allows for a player to add a custom tag onto their nickname, and also revert it back to normal if they wish
This example allows for a player to add a custom tag onto their nickname, and also revert it back to normal if they wish
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function addPlayerCustomTag ( player, command, newTag )
function addPlayerCustomTag ( thePlayer, command, newTag )
--Lets check that the newTag param has been entered...
--Let's make sure the newTag param has been entered...
if ( newTag ) then
if ( newTag ) then
--Grab their current playername for saving.
--Grab their current playername for saving.
local sPlayerNickname = getClientName(player)
local sPlayerNickname = getClientName ( thePlayer )
--Create their new nickname with their tag
--Create their new nickname with their tag
local sNewPlayerNickname = newTag .. " " .. sPlayerNickname
local sNewPlayerNickname = newTag .. " " .. sPlayerNickname
--Lets first load the element data, see if its there allready
--Let's first load the element data, see if it's there already
--The reason for this is that if a player were to do /addtag twice
--The reason for this is that if a player were to do /addtag twice,
--it would have saved their last tag as their orignal nickname
--the tag would be prepended a second time
--without this check
local sOldNick = getElementData( thePlayer, "tempdata.originalnick" )
local sOldNick = getElementData( player, "tempdata.orignalnick" )
if ( sOldNick == false ) then
if ( sOldNick == false ) then
--Save their orignal nickname in their element data
--Save their orignal nickname in their element data
setElementData ( player, "tempdata.orignalnick", sPlayerNickname )
setElementData ( thePlayer, "tempdata.originalnick", sPlayerNickname )
end
end
--Set their new nickname globally
--Set their new nickname globally
setClientName ( player, sNewPlayerNickname )
setClientName ( thePlayer, sNewPlayerNickname )
--Tell them its done
--Tell them it's done
outputChatBox ( "Your new nickname has been set, to put it back to its orignal state you can use /deltag", player )
outputChatBox ( "Your new nickname has been set, to put it back to its original state you can use /deltag", thePlayer )
else
else
--The newTag param was not entered, give a error message
--The newTag param was not entered, give an error message
outputChatBox ( "/addtag - Incorrect syntax, Correct: /addtag <newtag>", player )
outputChatBox ( "/addtag - Incorrect syntax, Correct: /addtag <newtag>", thePlayer )
end
end
end
end
addCommandHandler ( "addtag", addPlayerCustomTag )
addCommandHandler ( "addtag", addPlayerCustomTag )


function removePlayerCustomTag ( player, command )
function removePlayerCustomTag ( thePlayer, command )
--We first need to check that they have allready used /addtag, lets do that now
--We first need to check that they have already used /addtag, let's do that now
local sOldNick = getElementData( player, "tempdata.orignalnick" )
local sOldNick = getElementData( thePlayer, "tempdata.originalnick" )
if ( sOldNick ) then
if ( sOldNick ) then
--Great, they have a tag added, lets reset them
--Great, they have a tag added, let's reset them
--First we will want to reset the element data back to its default (that being false)
--First we will want to reset the element data back to its default (that being false)
setElementData ( player, "tempdata.orignalnick", false )
setElementData ( thePlayer, "tempdata.originalnick", false )
--Now set the client name back
--Now set the client name back
setClientName ( player, sOldNick )
setClientName ( thePlayer, sOldNick )
--Notify them
--Notify them
outputChatBox ( "Your old nickname has been set", player )
outputChatBox ( "Your old nickname has been set", thePlayer )
end
end
end
end

Revision as of 12:32, 16 August 2007

This function stores data attached to an element. This data can be any primitive LUA type or MTA element (but not a text item or display).

Element data is a useful way to store data attached to players.

Syntax

bool setElementData ( element theElement, string key, var value ) 

Required Arguments

  • theElement: The element you wish to attach the data to
  • key: The key you wish to store the data under
  • value: The value you wish to store

Returns

Returns true if the data was set succesfully, false otherwise.

Example

Click to expand [+]
Server

See Also