Jump to content

destroyElement with time "onVehicleEnter/onVehicleExit"


#RooTs

Recommended Posts

Hello friends. I did a panel to create vehicles, and after 5 seconds after I created. I want it to be destroyed.

and I want the time to stop, after I enter the vehicle

my attempt

-- onDestroyVehicle 
---------------------------------------------------------------------------------------- 
            addEventHandler("onVehicleEnter", veiculo[source],  
            function()  
            if isTimer(veiculo[source]) then  
            killTimer(veiculo[source])  
            end  
            end) 
  
            addEventHandler("onVehicleExit", veiculo[source],  
            function()  
            veiculo[source] = setTimer(function(source)  
            if isPedInVehicle(source)== false then  
            if ( isElement ( veiculo[source] ) ) then 
            destroyElement ( veiculo[source] ) 
            end 
            end 
            end, 5000, 1, source) -- arrume o tempo em milisegundos nessa linha tempo atual ? "5000" 
            end) 

part of my script that is working perfect

function destroirCarro() 
    if veiculo[source] and isElement(veiculo[source]) then 
        destroyElement(veiculo[source]) 
    end 
end 
addEventHandler("onPlayerLogout", getRootElement(), destroirCarro) 
addEventHandler("onPlayerQuit", getRootElement(), destroirCarro) 
addEventHandler("onPlayerWasted", getRootElement(), destroirCarro) 
  
-- onVehicleExplode 
function VehicleExplode() 
    local isCreatedByPlayer = false; 
  
    for k, v in pairs( veiculo ) do 
        if ( v == source ) then 
            isCreatedByPlayer = k; 
            break; 
        end 
    end 
  
    if ( isCreatedByPlayer ) then 
        setTimer ( 
            function( vehicle, owner ) 
                destroyElement( vehicle ) 
                veiculo[owner] = nil 
            end 
        , 5000, 1, source, isCreatedByPlayer ) 
    end 
end 
addEventHandler("onVehicleExplode", root, VehicleExplode) 

Link to comment
  • Moderators

In some parts of your code, veiculo[owner] is used as a vehicle, and, in some other parts, as a timer which doesn't make sense.

I would go like this:

-- onDestroyVehicle 
---------------------------------------------------------------------------------------- 
  
addEventHandler("onVehicleExit", root, 
function() 
    if not isVehicleCreatedByPlayer( source ) or not isVehicleEmpty( vehicle ) then 
        return -- It's not a vehicle created by a player or the vehicle is not empty then cancel 
    end 
     
    local destroyTimer = setTimer( 
        function( vehicle ) 
            if isElement( vehicle ) then 
                destroyElement( vehicle ) 
            end 
        end, 
    5000, 1, vehicle) 
    setElementData(vehicle, "destroyTimer", destroyTimer) 
end) 
  
addEventHandler("onVehicleEnter", root, 
function() 
    if not isVehicleCreatedByPlayer( source ) then return end 
     
    local destroyTimer = getElementData(source, "destroyTimer") 
    if isTimer(destroyTimer) then 
        killTimer(destroyTimer) 
        removeElementData(source, "destroyTimer") 
    end 
end) 
  
function getVehicleCreatedByPlayer( player ) 
    for p, v in pairs( veiculo ) do 
        if p == player then return v end 
    end 
    return nil 
end 
  
function getVehicleOwner( vehicle ) 
    for p, v in pairs( veiculo ) do 
        if v == vehicle then return p end 
    end 
    return nil 
end 
  
function isVehicleCreatedByPlayer( vehicle ) 
    local owner = getVehicleOwner( vehicle ) 
    return owner and getElementType(owner) == "player" 
end 
  
-- From [url=https://wiki.multitheftauto.com/wiki/IsVehicleEmpty]https://wiki.multitheftauto.com/wiki/IsVehicleEmpty[/url] 
function isVehicleEmpty( vehicle ) 
    if not isElement( vehicle ) or getElementType( vehicle ) ~= "vehicle" then 
        return true 
    end 
    local passengers = getVehicleMaxPassengers( vehicle ) 
    if type( passengers ) == 'number' then 
        for seat = 0, passengers do 
            if getVehicleOccupant( vehicle, seat ) then 
                return false 
            end 
        end 
    end 
    return true 
end 
  
----------------------- 
  
function destroirCarro() 
    local vehicle = getVehicleCreatedByPlayer( source ) 
    if vehicle and isElement( vehicle ) then 
        destroyElement( vehicle ) 
    end 
end 
addEventHandler("onPlayerLogout", root, destroirCarro) 
addEventHandler("onPlayerQuit", root, destroirCarro) 
addEventHandler("onPlayerWasted", root, destroirCarro) 
  
-- onVehicleExplode 
function VehicleExplode() 
    if not isVehicleCreatedByPlayer( source ) then return end 
     
    local owner = getVehicleOwner( source ) 
    setTimer( 
        function( vehicle, owner ) 
            destroyElement( vehicle ) 
            veiculo[owner] = nil 
        end, 
    5000, 1, source, owner ) 
end 
addEventHandler("onVehicleExplode", root, VehicleExplode) 

Link to comment
addEventHandler("onVehicleExit", root, 
function() 
    if not isVehicleCreatedByPlayer( source ) or not isVehicleEmpty( source ) then 
        return -- It's not a vehicle created by a player or the vehicle is not empty then cancel 
    end 
    
    local destroyTimer = setTimer( 
        function( vehicle ) 
            if isElement( vehicle ) then 
                destroyElement( vehicle ) 
            end 
        end, 
    5000, 1, source) -- vehicle is not defined here, so i wrote source instead of 'vehicle' 
    setElementData(source, "destroyTimer", destroyTimer) -- same 
end) 

Link to comment
-- onDestroyVehicle 
---------------------------------------------------------------------------------------- 
  
addEventHandler("onVehicleExit", root, 
function() 
    if not isVehicleCreatedByPlayer(source) or not isVehicleEmpty(source) then 
        return --It's not a vehicle created by a player or the vehicle is not empty then cancel 
    end 
     
    local destroyTimer = setTimer( 
        function(vehicle) 
            if isElement(vehicle) then 
                destroyElement(vehicle) 
            end 
        end, 
    5000, 1, source) 
    setElementData(source, "destroyTimer", destroyTimer) 
end) 
  
addEventHandler("onVehicleEnter", root, 
function() 
   if not isVehicleCreatedByPlayer(source) then return end 
    
    local  destroyTimer = getElementData(source, "destroyTimer") 
    if isTimer(destroyTimer) then 
       killTimer(destroyTimer) 
       removeElementData(source, "destroyTimer") 
    end 
end) 
  
function getVehicleCreatedByPlayer(player) 
    for p,v in (veiculo) do 
        if p == player then return v end 
    end 
    return nil 
end 
  
function getVehicleOwner(vehicle) 
    for p, v in pairs(veiculo) do 
        if v == vehicle then return p end 
    end 
    return nil 
end 
  
function isVehicleCreatedByPlayer(vehicle) 
    local owner = getVehicleOwner(vehicle) 
    return owner and getElementType(owner) =="player" 
end 
  
-- From [url=https://wiki.multitheftauto.com/wiki/isVehicleEmpty]https://wiki.multitheftauto.com/wiki/isVehicleEmpty[/url] 
function isVehicleEmpty(vehicle) 
   if not isElement(vehicle) or getElementType(vehicle) ~= "vehicle" then 
       return true 
   end 
   local passengers = getVehicleMaxPassengers(vehicle) 
   if type(passengers) == "number" then 
       for seat = 0, passengers do 
           if getVehicleOccupant(vehicle, seat) then 
              return false 
           end 
       end 
   end 
   return true 
end 
  
----------------------- 
  
function destoirCarro() 
    local vehicle = getVehicleCreatedByPlayer(source) 
    if vehicle and isElement(vehicle) then 
       destroyElement(vehicle) 
    end 
end 
addEventHandler("onPlayerLogout", root, destoirCarro) 
addEventHandler("onPlayerQuit", root, destoirCarro) 
addEventHandler("onPlayerWasted", root, destoirCarro) 
  
-- onVehicleExplode 
function VehicleExplode() 
    if not isVehicleCreatedByPlayer(source) then return end 
     
    local owner = getVehicleOwner(source) 
    setTimer( 
        function(vehicle, owner) 
            destroyElement(vehicle) 
            veiculo[owner] = nil 
        end, 
    5000, 1, source, owner) 
end 
addEventHandler("onVehicleExplode", root, VehicleExplode) 

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