Use a table to recognize if the key has been pressed to attach the trailer. if it is pressed it will allow it to attach. If it isnt attached it will detach the trailer.
-- just a table
trailers = {}
-- use this when creating the trailer
trailer = createVehicle ( int model, float x, float y, float z)
trailers[player] = false
-- use this when you press the key
trailers[source] = true
function detachTrailer(theTruck)
if trailers[trailer] == false then
--detachTrailerFromVehicle(theTruck, source) --detach the newly attached trailer
-- Immediate detatchment of the trailer through cancel event or this method doesn't seem to work so requires a timer:
setTimer(detachTrailer2, 50, 1, theTruck, source)
end
addEventHandler("onTrailerAttach", getRootElement(), detachTrailer)
function detachTrailer2(theTruck, trailer)
if (isElement(theTruck) and isElement(trailer)) then
detachTrailerFromVehicle(theTruck, trailer)
end
end