NoviceWithManyProblems

Expected element at argument 1.

Recommended Posts

I wanted to do something like this: cYVfe4L.png

and i have db3 error: c3QTo3g.png

(nice cut)

code:


addCommandHandler("test", function(plr, cmd)
    local x, y, z = getElementPosition(plr)
    obj = createObject(1234, x, y, z)
    
    addEvent("trigger", true)
    addEventHandler("trigger", root, function()
        destroyElement(obj)
    end)
end)

 

Share this post


Link to post
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

Share this post


Link to post
57 minutes ago, NoviceWithManyProblems said:

does not work, the object does not create at all, no errors in db3

I can't detect any issues with the code after a quick look. Are you sure you tested it correctly?

Share this post


Link to post

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.

 

Share this post


Link to post
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

Share this post


Link to post

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.