Einheit-101 Posted September 28, 2014 Posted September 28, 2014 Hello Community! I got a little problem and i dont know why. I made this script: I tested it but it does nothing! I dont know why, but the Event does not even trigger. No debug errors, nothing. It just happens nothing. The script is definitely running (There is other code in the .lua file which works). Can somebody please find the error? Thanks local areas = {} function joyn ( ) local objects = getElementsByType("object") for i, v in ipairs( objects ) do if (getElementModel(v) == 1381) then local col = createColSphere ( 0, 0, 0, 2 ) attachElements ( col, v, 0, 0, 1 ) setElementData(col, "type", "mine") table.insert(areas, col) end end end addEventHandler ( "onResourceStart", getResourceRootElement(getThisResource()), joyn ) function colHit( theElement ) outputChatBox("hit") if (getElementData(source, "type") == "mine") then if (getElementType ( theElement ) == "vehicle") then outputChatBox("boat") local elem = getAttachedElements(source) for i, v in ipairs do if (getElementModel(v) == 1381) then local x,y,z = getElementPosition ( v ) createExplosion ( x,y,z, 8 ) destroyElement ( elem ) destroyElement ( source ) end end end end end addEventHandler ( "onResourceStart", getResourceRootElement(getThisResource()), function ( ) for _, area in ipairs ( areas ) do addEventHandler ( 'onColShapeHit', area, colHit ) end end )
Einheit-101 Posted September 28, 2014 Author Posted September 28, 2014 once i deleted all stuff in colHit and left only outputChatBox for testing if it gets triggered. But even this does not work.
#DRAGON!FIRE Posted September 28, 2014 Posted September 28, 2014 try this : local areas = { } function joyn ( ) local objects = getElementsByType( "object" ) for i, v in ipairs( objects ) do if ( getElementModel( v ) == 1381 ) then local col = createColSphere ( 0, 0, 0, 2 ) attachElements ( col, v, 0, 0, 1 ) setElementData ( col, "type", "mine" ) table.insert ( areas, col ) end end for _, area in ipairs ( areas ) do addEventHandler ( 'onColShapeHit', area, colHit ) end end addEventHandler ( "onResourceStart", resourceRoot, joyn ) function colHit ( theElement ) if ( getElementType ( theElement ) == "vehicle" ) then if ( getElementData(source, "type") == "mine" ) then if ( getAttachedElements( source ) ) then for _,v in ipairs ( getAttachedElements( source ) ) do if ( getElementModel( v ) == 1381 ) then local x, y, z = getElementPosition ( v ) createExplosion ( x, y, z, 8 ) destroyElement ( v ) destroyElement ( source ) end end end end end end
Anubhav Posted September 28, 2014 Posted September 28, 2014 try this : local areas = { } function joyn ( ) local objects = getElementsByType( "object" ) for i, v in ipairs( objects ) do if ( getElementModel( v ) == 1381 ) then local col = createColSphere ( 0, 0, 0, 2 ) attachElements ( col, v, 0, 0, 1 ) setElementData ( col, "type", "mine" ) table.insert ( areas, col ) end end for _, area in ipairs ( areas ) do addEventHandler ( 'onColShapeHit', area, colHit ) end end addEventHandler ( "onResourceStart", resourceRoot, joyn ) function colHit ( theElement ) if ( getElementType ( theElement ) == "vehicle" ) then if ( getElementData(source, "type") == "mine" ) then if ( getAttachedElements( source ) ) then for _,v in ipairs ( getAttachedElements( source ) ) do if ( getElementModel( v ) == 1381 ) then local x, y, z = getElementPosition ( v ) createExplosion ( x, y, z, 8 ) destroyElement ( v ) destroyElement ( source ) end end end end end end You will waste his CPU on adding event handler by one more loop lol. local areas = { } function joyn ( ) local objects = getElementsByType( "object" ) for i, v in ipairs( objects ) do if ( getElementModel( v ) == 1381 ) then local col = createColSphere ( 0, 0, 0, 2 ) attachElements ( col, v, 0, 0, 1 ) setElementData ( col, "type", "mine" ) table.insert ( areas, col ) addEventHandler ( 'onColShapeHit', col, colHit ) end end end end addEventHandler ( "onResourceStart", resourceRoot, joyn ) function colHit ( theElement ) if ( getElementType ( theElement ) == "vehicle" ) then if ( getElementData(source, "type") == "mine" ) then if ( getAttachedElements( source ) ) then for _,v in ipairs ( getAttachedElements( source ) ) do if ( getElementModel( v ) == 1381 ) then local x, y, z = getElementPosition ( v ) createExplosion ( x, y, z, 8 ) destroyElement ( v ) destroyElement ( source ) end end end end end end
#DRAGON!FIRE Posted September 28, 2014 Posted September 28, 2014 u understand what u talk ? I corrected his code only .. if u insert Colshape in table and make loop for this table .. is not problem -__-
Anubhav Posted September 28, 2014 Posted September 28, 2014 u understand what u talk ?I corrected his code only .. if u insert Colshape in table and make loop for this table .. is not problem -__- Oh woah, you correct IT ONLY. You don't care if it makes lag and use alot of RAM right? Then don't talk. Whenever you create it attach it there only instead of making loops and wasting the energy.
#DRAGON!FIRE Posted September 28, 2014 Posted September 28, 2014 You don't care if it makes lag and use alot of RAM right? bro this don't makes lag Lol but your way is good .. but his way don't makes lag .. cus he insert ( Objects ) in table and make a loop and get all objects test the code by yourself and tell me if u got lag .
Anubhav Posted September 28, 2014 Posted September 28, 2014 You don't care if it makes lag and use alot of RAM right? bro this don't makes lag Lol but your way is good .. but his way don't makes lag .. cus he insert ( Objects ) in table and make a loop and get all objects test the code by yourself and tell me if u got lag . Your code will take more ticks = can create lag.
Einheit-101 Posted September 28, 2014 Author Posted September 28, 2014 Thank you all. I will try it asap. And please no dispute here €DIT::: Unfortunately, no one of your codes works It doesnt even trigger, as before
Moderators IIYAMA Posted October 2, 2014 Moderators Posted October 2, 2014 Debug your code manually, if you really want this to work. Unfortunately those who aren't doing that, are the ones who say that others are doing it wrong. While the real mistakes lies with them self, "forgetting to debug the code". Before you show us more of your "useless" conclusions. Visit this page: https://wiki.multitheftauto.com/wiki/Debugging It gives you more information about a way to find your problem. (THE BEST WAY) I/we will help you further after you debugged it and repost it.
Einheit-101 Posted October 2, 2014 Author Posted October 2, 2014 I found the issue. It was weird. This code does work: function joyn ( ) local objects = getElementsByType( "object" ) for i, v in ipairs( objects ) do if ( getElementModel( v ) == 1381 ) then local x, y, z = getElementPosition(v) local col = createColSphere ( x, y, z+1, 2.5 ) setElementData ( col, "object", v ) setElementData ( col, "type", "mine" ) addEventHandler ( 'onColShapeHit', col, colHit ) end end end addEventHandler ( "onResourceStart", resourceRoot, joyn ) function colHit ( theElement ) if ( getElementType ( theElement ) == "vehicle" ) then if ( getElementData(source, "type") == "mine" ) then local mine = getElementData ( source, "object" ) if ( getElementModel( mine ) == 1381 ) then local x, y, z = getElementPosition ( mine ) createExplosion ( x, y, z, 8 ) destroyElement ( source ) destroyElement ( mine ) end end end end There is only one small thing left, for i, v in ipairs( objects ) do if ( getElementModel( v ) == 1381 ) then does trigger twice. I dont know why, but it creates 2 colShapes for 1 object^^
Moderators IIYAMA Posted October 2, 2014 Moderators Posted October 2, 2014 Again a conclusion which could be solved by adding debugs. Your problems lays here: "setElementData ( col, "object", v )" Use setElementParent instead of setElementData. (when the parent get destroyed the child goes with it) Else yes you create double/tripple/quad colshapes when you restart the map.
Einheit-101 Posted October 2, 2014 Author Posted October 2, 2014 Funny: this issue does only occur in map editor I use it sometimes to test scripts and it caused this issue^^ I tried in a common environment without Editor and there is only 1 colshape.
Moderators IIYAMA Posted October 2, 2014 Moderators Posted October 2, 2014 Probably the editor buffer.
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