Jump to content

onColShapeHit does not trigger


Einheit-101

Recommended Posts

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 
) 

Link to comment

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

Link to comment
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.

Link to comment
You don't care if it makes lag and use alot of RAM right?

:lol::lol:

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.

Link to comment
  • Moderators

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. :!:

Link to comment

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^^

Link to comment
  • Moderators

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.

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...