Jump to content

When the vehicle stops touching any objects


HunT

Recommended Posts

Hi guys.  After a loooooong time I decided to do a rework on one of my map but I need a particular script. 

I have so forgotten anything related to scripting after 10 years and I don't know if there is any event or function related to this yet.

I need help creating a script when a certain vehicle stops touching an object after 5 seconds I have the ability to start a function. Such as destroying the vehicle after 5 seconds if it does not touch an object with its wheels.

If anyone helps me I will add credits for the script when I release the map.

Thanks for the support 😉

Link to comment
37 minutes ago, HunT said:

Hi guys.  After a loooooong time I decided to do a rework on one of my map but I need a particular script. 

I have so forgotten anything related to scripting after 10 years and I don't know if there is any event or function related to this yet.

I need help creating a script when a certain vehicle stops touching an object after 5 seconds I have the ability to start a function. Such as destroying the vehicle after 5 seconds if it does not touch an object with its wheels.

If anyone helps me I will add credits for the script when I release the map.

Thanks for the support 😉



 

local vehicleTouchData = {}

function onVehicleTouch(hitElement, matchingDimension)
    if getElementType(hitElement) == "vehicle" then
        local vehicle = hitElement
        vehicleTouchData[vehicle] = true
        if isTimer(vehicleTouchData[vehicle.."timer"]) then
            killTimer(vehicleTouchData[vehicle.."timer"])
            vehicleTouchData[vehicle.."timer"] = nil
            outputChatBox("Vehicle touched the object again, timer canceled.")
        end
    end
end
addEventHandler("onElementCollide", root, onVehicleTouch)

function onVehicleLeave(leftElement, matchingDimension)
    if getElementType(leftElement) == "vehicle" then
        local vehicle = leftElement
        vehicleTouchData[vehicle] = false
        vehicleTouchData[vehicle.."timer"] = setTimer(function()
            if not vehicleTouchData[vehicle] then
                if isElement(vehicle) then
                    destroyElement(vehicle)
                    outputChatBox("Vehicle did not touch for 5 seconds, destroyed.")
                end
            end
        end, 5000, 1)
    end
end
addEventHandler("onElementLeaveCollide", root, onVehicleLeave)

function onVehicleDestroy()
    vehicleTouchData[source] = nil
    if isTimer(vehicleTouchData[source.."timer"]) then
        killTimer(vehicleTouchData[source.."timer"])
    end
end
addEventHandler("onElementDestroy", root, onVehicleDestroy)

 

Link to comment
18 hours ago, Shady1 said:



 

local vehicleTouchData = {}

function onVehicleTouch(hitElement, matchingDimension)
    if getElementType(hitElement) == "vehicle" then
        local vehicle = hitElement
        vehicleTouchData[vehicle] = true
        if isTimer(vehicleTouchData[vehicle.."timer"]) then
            killTimer(vehicleTouchData[vehicle.."timer"])
            vehicleTouchData[vehicle.."timer"] = nil
            outputChatBox("Vehicle touched the object again, timer canceled.")
        end
    end
end
addEventHandler("onElementCollide", root, onVehicleTouch)

function onVehicleLeave(leftElement, matchingDimension)
    if getElementType(leftElement) == "vehicle" then
        local vehicle = leftElement
        vehicleTouchData[vehicle] = false
        vehicleTouchData[vehicle.."timer"] = setTimer(function()
            if not vehicleTouchData[vehicle] then
                if isElement(vehicle) then
                    destroyElement(vehicle)
                    outputChatBox("Vehicle did not touch for 5 seconds, destroyed.")
                end
            end
        end, 5000, 1)
    end
end
addEventHandler("onElementLeaveCollide", root, onVehicleLeave)

function onVehicleDestroy()
    vehicleTouchData[source] = nil
    if isTimer(vehicleTouchData[source.."timer"]) then
        killTimer(vehicleTouchData[source.."timer"])
    end
end
addEventHandler("onElementDestroy", root, onVehicleDestroy)

 

A thousand thanks. I will try this script 
Link to comment
  • 1 month later...

there are no such events as

onElementCollide
onElementLeaveCollide

https://wiki.multitheftauto.com/wiki/Client_Scripting_Events#Element_events

https://wiki.multitheftauto.com/wiki/Server_Scripting_Events#Element_events

I think the best thing you could do for this case is either a distance check, or creating collision shapes alongside the objects you need the check against. You maybe could achieve more precise results with a distance check logic, since you could get the position of a vehicle dummy or ped bone and many more, but you'd need to do it in a render event, or an event that is not as heavy as a render, but still constantly called, like onClientPedsProcessed.

Link to comment
  • 3 months later...
On 21/09/2024 at 13:15, Shady1 said:

I have not tested it, so please get back to me if there is anything missing or needs to be edited and I will help you

The truth, I had abandoned this project and did not have the opportunity to try your script. 

 

But today out of curiosity I asked chatGPT to write me the script and this is the result 

 

local vehicleTimers = {} -- Memorizza i timer dei veicoli

-- Funzione per controllare se il veicolo è in aria
function checkVehicleAir(vehicle)
    if not isElement(vehicle) then return end
    if isVehicleOnGround(vehicle) then
        resetExplosionTimer(vehicle)
    else
        startExplosionTimer(vehicle)
    end
end

-- Avvia il timer di esplosione
function startExplosionTimer(vehicle)
    if vehicleTimers[vehicle] then return end -- Evita più timer sullo stesso veicolo
    
    vehicleTimers[vehicle] = setTimer(function()
        if isElement(vehicle) and not isVehicleOnGround(vehicle) then
            blowVehicle(vehicle) -- 💥 Esplode il veicolo!
            vehicleTimers[vehicle] = nil
        end
    end, 5000, 1) -- 5 secondi di tempo
end

-- Resetta il timer se il veicolo tocca il suolo
function resetExplosionTimer(vehicle)
    if vehicleTimers[vehicle] then
        killTimer(vehicleTimers[vehicle]) -- Ferma il timer
        vehicleTimers[vehicle] = nil
    end
end

-- Controlla lo stato dei veicoli in un loop
function monitorVehicles()
    for _, vehicle in ipairs(getElementsByType("vehicle")) do
        if getVehicleController(vehicle) then -- Controlla solo i veicoli con giocatori dentro
            checkVehicleAir(vehicle)
        end
    end
end
setTimer(monitorVehicles, 1000, 0) -- Controlla ogni secondo

-- Quando un veicolo esplode, rimuove il timer associato
addEventHandler("onVehicleExplode", root, function()
    vehicleTimers[source] = nil
end)
chatGPT can write all kinds of functions, it's amazing 
Edited by HunT
  • Like 1
Link to comment
  • 3 weeks later...
On 2/18/2025 at 12:22 PM, HunT said:
The truth, I had abandoned this project and did not have the opportunity to try your script. 

 

But today out of curiosity I asked chatGPT to write me the script and this is the result 

 

local vehicleTimers = {} -- Memorizza i timer dei veicoli

-- Funzione per controllare se il veicolo è in aria
function checkVehicleAir(vehicle)
    if not isElement(vehicle) then return end
    if isVehicleOnGround(vehicle) then
        resetExplosionTimer(vehicle)
    else
        startExplosionTimer(vehicle)
    end
end

-- Avvia il timer di esplosione
function startExplosionTimer(vehicle)
    if vehicleTimers[vehicle] then return end -- Evita più timer sullo stesso veicolo
    
    vehicleTimers[vehicle] = setTimer(function()
        if isElement(vehicle) and not isVehicleOnGround(vehicle) then
            blowVehicle(vehicle) --  Esplode il veicolo!
            vehicleTimers[vehicle] = nil
        end
    end, 5000, 1) -- 5 secondi di tempo
end

-- Resetta il timer se il veicolo tocca il suolo
function resetExplosionTimer(vehicle)
    if vehicleTimers[vehicle] then
        killTimer(vehicleTimers[vehicle]) -- Ferma il timer
        vehicleTimers[vehicle] = nil
    end
end

-- Controlla lo stato dei veicoli in un loop
function monitorVehicles()
    for _, vehicle in ipairs(getElementsByType("vehicle")) do
        if getVehicleController(vehicle) then -- Controlla solo i veicoli con giocatori dentro
            checkVehicleAir(vehicle)
        end
    end
end
setTimer(monitorVehicles, 1000, 0) -- Controlla ogni secondo

-- Quando un veicolo esplode, rimuove il timer associato
addEventHandler("onVehicleExplode", root, function()
    vehicleTimers[source] = nil
end)
chatGPT can write all kinds of functions, it's amazing 

Well, I tried to use it, but GPT very stupid in code, really. And stupid by itself. You probably don't want to use it for real code, only for ideas or simple monkey job.

And it's loving to invent new functions in standard MTA, so you need to check it on wiki or in IDE with code highlighting. I guess it's because on MTA not so many free code in network. Maybe new versions will can do it better, but not now.

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