Jump to content

Can I ask for help with this code?


PeterP

Recommended Posts

function addCapacete (source)
    local capacete1 = getElementData(source, "capaceteOn")
    if capacete1 then
        exports.pAttach:detach(capacete1)
        destroyElement(capacete1)
        setElementData(source, "capaceteOn", nil)
    else
        capacete1 = createObject(1873,0,0,0)
        setElementData(source, "capaceteOn", capacete1)
        exports.pAttach:attach(capacete1, source, 8, -0.02,0,0,0,0,-104.4)
    end
end
addEvent ( "onaddCapacete", true )
addEventHandler("onaddCapacete", getRootElement(), addCapacete)

I'm getting an error: bad argument @  'destroyElement'  [expected element at argument 1] on line 5

why am I getting this error? 

Link to comment
if capacete1 then

This checks whether a value is assigned to capacete1 (this value could be anything -- a string, number, userdata, coroutine, element, etc.), not whether it is a valid value for the purposes of your script.

If the warning message you get says "expected element", it means you've passed it something that isn't an element (for instance, it could be a userdata reference to an element that's been destroyed by another script). A solution that gets rid of the warning is

if isElement(capacete1) then
Edited by Addlibs
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...