[HELP] Vehicle with attached trailer

So Im trying to spawn a certain vehicle with a trailer attached to it for a job farm. This is my current script that only spawns the vehicle with no trailer.

function spawntractor()

    Citizen.Wait(0)
    local player = PlayerId()
    local vehicle = GetHashKey('tracto5')
	local trailer = GetHashKey('GrainTrailer')
 
    RequestModel(vehicle)
 
    while not HasModelLoaded(vehicle) do
        Wait(1)
    end
 
	local plate = math.random(100, 900)
	local tractor = CreateVehicle(vehicle,2551.86,4680.95,33.89 , 18, true, false)
	SetVehicleOnGroundProperly(tractor)
	AttachVehicleToTrailer(vehicle, trailer, 1.1)
	local plate = "ZURRUPIO"..math.random(100, 900)
    SetVehicleNumberPlateText(tractor, plate)
	SetPedIntoVehicle(GetPlayerPed(-1), tractor, - 1)
	SetModelAsNoLongerNeeded(vehicle)
	Citizen.InvokeNative(0xB736A491E64A32CF, Citizen.PointerValueIntInitialized(tractor))
end 

If anyone knows how can I spawn it with and attached trailer please let me know. Thanks.

You have to use the CreateVehicle() native for the trailer as well. You should also Request and Load the model as you did with the tractor

Hi! Thanks for the answer. Im new programming at lua so I dont really know how to do that. Could you explain me a little bit how can I do it? Thanks!

Well here you are spawning in your tractor:

local tractor = CreateVehicle(vehicle,2551.86,4680.95,33.89 , 18, true, false)

You need to do the exact same thing with your trailer. You got the hash key of your trailer, but you never loaded the model and never spawned it in.
Do something like this:

    local trailer = GetHashKey('GrainTrailer')
    RequestModel(trailer)
 
    while not HasModelLoaded(trailer) do
        Wait(1)
    end
 
	local tractorTrailer = CreateVehicle(trailer,2551.86,4680.95,33.89 , 18, true, false)

nice good job !

Thanks man! It worked perfectly! :smiley:

happy I could help!