Jump to content

getDistance Trailer and Vehicle


Stijger

Recommended Posts

Hello again,

Apparently I really suck at scripting, so i need your help again guys.

I got this code, and what I want is if the trailer and truck get to far from eachother on trailerdetach

(so on like big high speed crash) they blow up.

function trailerGone ( theTruck ) 
    theTruckVehicle = theTruck 
    theTruckTrailer = source 
    trailerx, trailery, trailerz = getElementPosition(source) 
    vehiclex, vehicley, vehiclez = getElementPosition (theTruckVehicle) 
    outputChatBox ("X coordinate of trailer is " ..trailerx) 
    outputChatBox ("X coordinate of vehicle is " ..vehiclex) 
    dist = getDistanceBetweenPoints2D (vehiclex, vehicley, trialerx, trailery) 
    if(getDistanceBetweenPoints2D (vehiclex, vehicley, trialerx, trailery) <= 20) then 
        blowVehicle ( theTruckVehicle ) 
        blowVehicle ( theTruckTrailer ) 
    end 
end 
addEventHandler( "onTrailerDetach", getRootElement(), trailerGone ) 
  

Gives these errors:

WARNING: race-convoytrucking\trailerattach.lua:8: Bad argument @ 'getDistanceBetweenPoints2D'

WARNING: race-convoytrucking\trailerattach.lua:9: Bad argument @ 'getDistanceBetweenPoints2D'

ERROR: race-convoytrucking\trailerattach.lua:9: attempt to compare boolean with number

Now the problem is, I get a bad argument on the trailerx when

it's compared to the vehiclex. (same with the y coordinates i guess)

the outputbox trailerx and vehiclex is working fine thou, so why doesn't it work in the getDistanceBetweenPoints2D?

edit: the source and theTruckTrailer part I switched already, don't make any difference.

Link to comment
function trailerGone ( uTruck ) 
    local trailerx, trailery, _ = getElementPosition( source ) 
    local vehiclex, vehicley, _ = getElementPosition ( uTruck ) 
    outputChatBox ( "X coordinate of trailer is " .. tostring( trailerx ) ) 
    outputChatBox ( "X coordinate of vehicle is " .. tostring( vehiclex ) ) 
    local dist = getDistanceBetweenPoints2D ( vehiclex, vehicley, trailerx, trailery ) 
    if dist <= 20 then 
        blowVehicle ( uTruck ) 
        blowVehicle ( source ) 
    end 
end 
addEventHandler( "onTrailerDetach", root, trailerGone ) 
  

Because you use not defined variable trialerx in 9 line.

Link to comment

No problem.

In old version mta server/client outputChatBox/outputDebugString not convert all variables to string.

You need use tostring in function outputChatBox/outputDebugString for convert variables to string.

If you not convert, it return false and bad argument and not output.

Link to comment
  • 1 month later...

Hey,

I need some help again.

Iam still working on the trailer distance thing,

but now i thought well wouldn't it be more awesome

if you can dettach the trailer, but when you get too far from it,

you blow up.

So something like that checks the distance between the two vehicles

a lot and then when you get too far, it goes BOOM!!

Tried this but it don't remember the trailer after the dettachment.

root = getRootElement () 
function attachedTrailer () 
    local theTruckVehicle = getPedOccupiedVehicle ( getLocalPlayer () ) 
    local theTruckTrailer = getVehicleTowedByVehicle ( theTruckVehicle ) 
    local vehiclex, vehicley, vehiclez = getElementPosition (theTruckVehicle) 
    local trailerx, trailery, trailerz = getElementPosition(theTruckTrailer) 
    local dist = getDistanceBetweenPoints2D (trailerx, trailery, vehiclex, vehicley) 
    if dist >= 20 then 
        blowVehicle ( theTruckVehicle ) 
        blowVehicle ( theTruckTrailer ) 
    end 
end 
addEventHandler ( "onClientRender", root, attachedTrailer ) 

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