I solved it.
function job_trailerAttach(veh)
cancelEvent()
local x, y, z = getElementPosition(veh)
local x2, y2, z2 = getElementPosition(source)
local _,_,rz=getElementRotation(veh)
local rrz=math.rad(rz)
local x= x + (2 * math.sin(-rrz))
local y= y + (2 * math.cos(-rrz))
detachTrailerFromVehicle(veh, source)
setElementPosition(veh, x, y, z)
setElementPosition(source, x2, y2, z2)
end
addEventHandler("onTrailerAttach", getRootElement(), job_trailerAttach)
Thank you all for help.