Jump to content

why doesn't this easy code work?


xTravax

Recommended Posts

  
addEventHandler("onPlayerWasted",root, 
   function() 
      destroyElement(getPedOccupiedVehicle(source)) 
   end 
) 

this doesn't work

it says expected element at argument 1, got boolean

can someone explain why this most easiest code in the universe doesn't work?

i guess getPedOccupiedVehicle returns false when driver is dead, is there any workaround for this?

Link to comment

try this i did not test i just made it

addEventHandler( "onPlayerWasted", getRootElement( ), 
    function() 
      destroyElement(getPedOccupiedVehicle(source)) 
    end 
) 

or try

function onWasted() 
destroyElement(getPedOccupiedVehicle(source)) 
end 
  
-- add the event handler 
addEventHandler("onClientPedWasted", getRootElement(), onWasted) 

Edited by Guest
Link to comment

What happens when player is not inside a vehicle and dies? Thats right, getPedOccupiedVehicle() will return false and thus destroyElement() will also fail to execute.

addEventHandler("onPlayerWasted", root, function() 
    local veh = getPedOccupiedVehicle(source) 
    if veh then   
      destroyElement(veh) 
    end 
end ) 

Btw:

getRootElement() is old and root is getRootElement()

There is no difference between getRootElement() and root.

Link to comment

Oh god.

@naser that is same code and it wouldn't work

@dimos that's the same code with an error, using serverside function in client event (which also is unnecessary because the player is dead for sure)

@dakilla i'm in a vehicle, i wouldn't make this topic if i weren't in a vehicle, why would i even want to destroy an un-existant vehicle anyway?

Link to comment

Oh, yea, sorry. I just found out that there is similar bug report on mantis: https://bugs.mtasa.com/view.php?id=8242

Well, idk, you could make a workaround with element datas, like.. Set player data that he has entered/left the specified vehicle.

addEventHandler( "onPlayerVehicleEnter", root, function( veh, seat ) 
    if seat == 0 then 
        setElementData( source, "occupied_veh", veh, false ) 
    end 
end ) 
  
addEventHandler( "onPlayerVehicleExit", root, function( veh, seat ) 
    if seat == 0 and getElementData( source, "occupied_veh" ) == veh then 
        setElementData( source, "occupied_veh", nil, false ) 
    end 
end ) 
  
-- And here goes onPlayerWasted event: 
addEventHandler( "onPlayerWasted", root, function() 
    local occupied_veh = getElementData( source, "occupied_veh" ) 
     
    if occupied_veh and isElement( occupied_veh ) then 
        destroyElement( occupied_veh ) 
    end 
  
    setElementData( source, "occupied_veh", nil, false )     
end ) 

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