Jump to content

Expected element at argument 1.


Recommended Posts

  • Moderators
local obj

addCommandHandler("test", function(plr, cmd)
	if not isElement(obj) then
		local x, y, z = getElementPosition(plr)
		obj = createObject(1234, x, y, z)
	else
		outputChatBox("'obj' already created, destroy it first.", root)
	end
end)


addEvent("trigger", true)
addEventHandler("trigger", root, function()
	if isElement(obj) then
		destroyElement(obj)
	else
		outputChatBox("'obj' not found, create it first.", root)
	end
end)

 

  • Like 1
Link to comment

From what I have come to understand is, when you type 'test', you want to create an object, and if it already exists, then delete it. Imo you don't even need an event to do that, but if you want to do it your way, it would be better to pass the object as an argument to the trigger event.

local obj

addEventHandler("test", function(plr, cmd)
    if not isElement(obj) then
        obj = createObject(1234, getElementPosition(plr)) -- spawn object with '1234' id at our current position.
    else
        triggerEvent("deleteObject", obj)
    end
end)

addEvent("deleteObject", true)
addEventHandler("deleteObject", function(object)
    if not isElement(object) then
        return outputChatBox("Object does not exist.", source, 255, 0)
    end
    destroyElement(object)
    end
end)

An alternative and much simpler way to do this would be like this (but of course it depends on how you intend your script to work).

local obj

addEventHandler("test", function(plr, cmd)
    if not isElement(obj) then
        obj = createObject(1234, getElementPosition(plr)) -- spawn object with '1234' id at our current position.
    else
        outputChatBox("Destroying object.", plr)
        destroyElement(obj)
    end
end)

-----------

@stPatrick, with all due respect, where did you trigger the 'trigger' event? Custom events have to be manually triggered.

 

Link to comment
  • Moderators
4 minutes ago, Bilal135 said:

@stPatrick, with all due respect, where did you trigger the 'trigger' event? Custom events have to be manually triggered.

That was already in the code from the beginning. You are asking the wrong person for that information.

  • Like 2
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...