Jump to content

on(Client)ElementDataChange


pa3ck

Recommended Posts

Hi there, I'm having trouble with my little code. It works, but for some reason it spams the chat and gives me an error saying that "C stack overflow"

addEventHandler("onClientElementDataChange", root, function ( name, oldValue )  
    if getElementType(source) == "player" then 
        local protected = getElementData(source, "protected") or 0 
        if protected == 0 then 
            outputChatBox("Element data was changed back to old value.", 255, 0, 0) 
            newValue = getElementData(source, name, value) 
            setElementData(source, name, oldValue) 
        end 
    end 
end) 

Any help?

Link to comment

Calling 'setElementData' triggers "onElementDataChange"/"onClientElementDataChange". If these events cause 'setElementData' to be called, you get an infinite recursion. You could make a variable that stops the execution of event handler if element data is being reverted. An example:

local revertingData 
  
addEventHandler("onElementDataChange", root, function(dataname, oldvalue) 
    if revertingData then 
        return 
    end 
  
    revertingData = true 
    setElementData(source, dataname, oldvalue) 
    revertingData = nil 
end) 

'setElementData' call in this handler will cause the same handler to be called recursively, but the checking of 'revertingData' variable will prevent the subsequent recursive calls.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...