NoviceWithManyProblems Posted January 14, 2020 Share Posted January 14, 2020 I wanted to do something like this: and i have db3 error: (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) Link to comment
Moderators Patrick Posted January 14, 2020 Moderators Share Posted January 14, 2020 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) 1 Link to comment
NoviceWithManyProblems Posted January 14, 2020 Author Share Posted January 14, 2020 does not work, the object does not create at all, no errors in db3 Link to comment
Moderators IIYAMA Posted January 14, 2020 Moderators Share Posted January 14, 2020 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? Link to comment
Bilal135 Posted January 14, 2020 Share Posted January 14, 2020 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 IIYAMA Posted January 14, 2020 Moderators Share Posted January 14, 2020 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. 2 Link to comment
Bilal135 Posted January 14, 2020 Share Posted January 14, 2020 @IIYAMA, ah yes, i misunderstood. He might have wanted to trigger the event somewhere else. 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now