Jump to content

Attach existing trailer to existing vehicle


ivan8065

Recommended Posts

Hello, I am trying to make script that would allow me connect existing trailer to existing vehicle. I am working with example from here https://wiki.multitheftauto.com/wiki/AttachTrailerToVehicle and I managed to make trailer spawn to existing car but I don't know how define trailer so it will connect trailer that is spawned nearby to my car. Thank you for any help.

 

setTimer(
	function()
		local vehicle = getPedOccupiedVehicle( localPlayer )
		local x,y,z = getElementPosition( vehicle )
		local rx,ry,rz = getElementRotation ( vehicle )
		local trailer = 
		attachTrailerToVehicle ( vehicle, trailer )   -- attach then
		outputChatBox("Connected.", 255, 0, 0)
	end, 50, 1)

 

Link to comment

Hey,

There is a "useful" function on the wiki to get the nearest vehicle to a player by specified max distance - getNearestVehicle.
You can use it for your script but it will need a check whether the vehicle type is "trailer".

Something like this:

function getNearestTrailer(player,distance)
	local lastMinDis = distance-0.0001
	local nearestTrailer = false
	local px,py,pz = getElementPosition(player)
	local pint = getElementInterior(player)
	local pdim = getElementDimension(player)

	for _,v in pairs(getElementsByType("vehicle")) do
		local vint,vdim = getElementInterior(v),getElementDimension(v)
		if vint == pint and vdim == pdim then
			local vx,vy,vz = getElementPosition(v)
			local dis = getDistanceBetweenPoints3D(px,py,pz,vx,vy,vz)
			if dis < distance then
				if dis < lastMinDis and getVehicleType(v) == "trailer" then 
					lastMinDis = dis
					nearestTrailer = v
				end
			end
		end
	end
	return nearestTrailer
end

And then use it like:

getNearestTrailer(localPlayer, 50)

 

Edited by SpecT
Link to comment

I hope I got it correctly like this

function getNearestTrailer(player,distance)
	local lastMinDis = distance-0.0001
	local nearestTrailer = false
	local px,py,pz = getElementPosition(player)
	local pint = getElementInterior(player)
	local pdim = getElementDimension(player)

	for _,v in pairs(getElementsByType("vehicle")) do
		local vint,vdim = getElementInterior(v),getElementDimension(v)
		if vint == pint and vdim == pdim then
			local vx,vy,vz = getElementPosition(v)
			local dis = getDistanceBetweenPoints3D(px,py,pz,vx,vy,vz)
			if dis < distance then
				if dis < lastMinDis and getVehicleType(v) == "trailer" then 
					lastMinDis = dis
					nearestTrailer = v
				end
			end
		end
	end
	return nearestTrailer
end

function hookTrailer(commandName)
		vehicle = getPedOccupiedVehicle(localPlayer)
		local x,y,z = getElementPosition( vehicle )
		local rx,ry,rz = getElementRotation ( vehicle )
		trailer = getNearestTrailer(localPlayer, 50)
		attachTrailerToVehicle ( vehicle, trailer )
		outputChatBox("Connected.", 255, 0, 0)
	end
addCommandHandler("hook", hookTrailer)

because I got warning

WARNING: trailer/trailer.lua:29: Bad argument @ 'attachTrailerToVehicle' [Expected vehicle at argument 2, got boolean]

 

Link to comment

It throws you this error cuz it couldn't find a trailer. Maybe the distance is too low (50) you can try by increasing it and test.

Edited the function a bit to prevent eventual errors.

function hookTrailer(commandName)
	local vehicle = getPedOccupiedVehicle(localPlayer)
  	if vehicle then
		local x,y,z = getElementPosition( vehicle )
		local rx,ry,rz = getElementRotation ( vehicle )
		local trailer = getNearestTrailer(localPlayer, 50)
		if trailer then
			attachTrailerToVehicle ( vehicle, trailer )
			outputChatBox("Connected.", 255, 0, 0)
		else
			outputChatBox("No nearby trailer was found!", 255, 0, 0)
		end
	else
		outputChatBox("You need to be in a vehicle!", 255, 0, 0)
	end
end
addCommandHandler("hook", hookTrailer)

*Edit: BTW I just thought about it that this probably is not gonna work as it is made in the client-side.
Vehicles created in client-side cannot be entered but only be shown.
So I guess your vehicles (truck and trailer) are spawned from server side ?
You will need to make the script server-sided. It's not hard but if you struggle don't hesitate to ask for assistance.

Edited by SpecT
Link to comment

I am trying to make it server-sided but in debug I have warnings from last 3 lines saying "Expected element at argument 1, got nil" and it is saying same thing if it's there player or thePlayer.

function getNearestTrailer(player,distance)
	local lastMinDis = distance-0.0001
	local nearestTrailer = false
	local px,py,pz = getElementPosition(player)
	local pint = getElementInterior(player)
	local pdim = getElementDimension(player)

 

Link to comment

Yeah sorry my fault. I changed it from localPlayer to thePlayer, even tried vehicle and now I don't have any errors or warnings but after using command it still wont attach trailer to vehicle and changing distance didn't help.

function getNearestTrailer(thePlayer,distance)
	local lastMinDis = distance-0.0001
	local nearestTrailer = false
	local px,py,pz = getElementPosition(thePlayer)
	local pint = getElementInterior(thePlayer)
	local pdim = getElementDimension(thePlayer)

	for _,v in pairs(getElementsByType("vehicle")) do
		local vint,vdim = getElementInterior(v),getElementDimension(v)
		if vint == pint and vdim == pdim then
			local vx,vy,vz = getElementPosition(v)
			local dis = getDistanceBetweenPoints3D(px,py,pz,vx,vy,vz)
			if dis < distance then
				if dis < lastMinDis and getVehicleType(v) == "trailer" then 
					lastMinDis = dis
					nearestTrailer = v
				end
			end
		end
	end
	return nearestTrailer
end

function hookTrailer(thePlayer,commandName)
	local vehicle = getPedOccupiedVehicle ( thePlayer )
  	if vehicle then
		local x,y,z = getElementPosition( vehicle )
		local rx,ry,rz = getElementRotation ( vehicle )
		local trailer = getNearestTrailer(thePlayer, 2000)
		if trailer then
			attachTrailerToVehicle ( vehicle, trailer )
			outputChatBox("Connected.", thePlayer, 0, 255, 0)
		else
			outputChatBox("No nearby trailer was found!", thePlayer, 255, 0, 0)
		end
	else
		outputChatBox("You need to be in a vehicle!", thePlayer, 255, 0, 0)
	end
end
addCommandHandler("hook", hookTrailer)

 

Link to comment

Hey,

I had to test it in-game and found what was wrong - getVehicleType returns the type as string but capitalized (lol).

Replace with:

getVehicleType(v) == "Trailer"

Also you will need to lower the distance cuz 2000 is way too much (tested it with 200 and it was attaching it from far). ?

Edited by SpecT
  • Thanks 1
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...