OnElementDataChange: Difference between revisions
Jump to navigation
Jump to search
Fernando187 (talk | contribs) m (formatting for global parameters) |
(Added info about being able to cancel the event.) |
||
Line 18: | Line 18: | ||
==Cancelling== | ==Cancelling== | ||
This event cannot be cancelled using [[cancelEvent]]. To reverse the effect, use [[setElementData]] with the old value. See Example. | This event cannot be cancelled using [[cancelEvent]]. To reverse the effect, use [[setElementData]] with the old value. See Example. | ||
{{New items|3.0170|1.7.0| | |||
This event can be cancelled using [[cancelEvent]]. If a client sent the cancelled change to the server, the server will send the client the server's version of the element data. | |||
|25731}} | |||
==Source== | ==Source== | ||
Line 42: | Line 46: | ||
outputChatBox("Illegal setting of " .. tostring(theKey) .. "' by '" .. tostring(getPlayerName(client))) | outputChatBox("Illegal setting of " .. tostring(theKey) .. "' by '" .. tostring(getPlayerName(client))) | ||
setElementData(source, theKey, oldValue) -- Set back the original value | setElementData(source, theKey, oldValue) -- Set back the original value | ||
end | |||
end | |||
addEventHandler("onElementDataChange", root, checkChange) | |||
</syntaxhighlight> | |||
This example blocks all element data changes from clients. In MTA 1.7+ only. | |||
<syntaxhighlight lang="lua"> | |||
function checkChange(theKey, oldValue, newValue) | |||
if (client) then -- if there's a client variable, it means it came from a player. | |||
cancelEvent() | |||
end | end | ||
end | end |
Latest revision as of 19:30, 7 October 2025
This event is triggered after an element's data entry is changed. Such changes can be made on the client or the server using setElementData.
Parameters
string theKey, var oldValue, var newValue
- theKey: The name of the element data entry that has changed.
- oldValue: The old value of this entry before it changed. See element data for a list of possible datatypes.
- newValue: the new value of this entry after it changed. This will be equivalent to getElementData(source, theKey).
Cancelling
This event cannot be cancelled using cancelEvent. To reverse the effect, use setElementData with the old value. See Example.
Source
The source of this event is the element whose element data changed.
Example
Click to collapse [-]
ServerThis example outputs a message to players when any of their element data values is changed.
function outputChange(theKey, oldValue, newValue) if (getElementType(source) == "player") then -- check if the element is a player outputChatBox("Your element data '" .. tostring(theKey) .. "' has changed from '" .. tostring(oldValue) .. "' to '" .. tostring(newValue) .. "'", source) -- output the change for the affected player end end addEventHandler("onElementDataChange", root, outputChange)
Click to collapse [-]
ServerThis example checks and possibly reverses an element's data change.
function checkChange(theKey, oldValue) -- The client can only set 'special_thing' on its own player if (theKey== "special_thing") and (client ~= source) then outputChatBox("Illegal setting of " .. tostring(theKey) .. "' by '" .. tostring(getPlayerName(client))) setElementData(source, theKey, oldValue) -- Set back the original value end end addEventHandler("onElementDataChange", root, checkChange)
This example blocks all element data changes from clients. In MTA 1.7+ only.
function checkChange(theKey, oldValue, newValue) if (client) then -- if there's a client variable, it means it came from a player. cancelEvent() end end addEventHandler("onElementDataChange", root, checkChange)
See Also
Element events
- onElementClicked
- onElementColShapeHit
- onElementColShapeLeave
- onElementDataChange
- onElementDestroy
- onElementDimensionChange
- onElementInteriorChange
- onElementModelChange
- onElementStartSync
- onElementStopSync
Event functions
- addEvent
- addEventHandler
- cancelEvent
- cancelLatentEvent
- getEventHandlers
- getLatentEventHandles
- getLatentEventStatus
- removeEventHandler
- triggerEvent
- wasEventCancelled