Jump to content

[HELP] Disable automatic trailer attach


BenceDev

Recommended Posts

You can use the onTrailerAttach event and detach the trailer as soon as this event is triggered. The only downside is you cannot simply cancel the event or detach the trailer immediately, instead you must use a timer that triggers after 50ms for it to be effective.

function detachTrailer(theVehicle)
    local theTrailer = source
    setTimer(function()
        if isElement(theVehicle) and isElement(theTrailer) then
            detachTrailerFromVehicle(theVehicle, theTrailer)
        end
    end, 50, 1)
end
addEventHandler("onTrailerAttach", root, detachTrailer)

 

Edited by Skully
Link to comment
6 minutes ago, Skully said:

You can use the onTrailerAttach event and detach the trailer as soon as this event is triggered. The only downside is you cannot simply cancel the event or detach the trailer immediately, instead you must use a timer that triggers after 50ms.

function detachTrailer(theVehicle)
	local theTrailer = source
	setTimer(function()
		if isElement(theVehicle) and isElement(theTrailer) then
        	detachTrailerFromVehicle(theVehicle, theTrailer)
        end
	end, 50, 1)
end
addEventHandler("onTrailerAttach", root, detachTrailer)

 

Okay i try it

Update: So i tried it, but when i attach the trailer with bind, after i attach the trailer is immediately get deattached, after attached and again deattach and this is go loop.

How can i fix this issue?

Edited by SphinxDev
Link to comment
1 hour ago, SphinxDev said:

Okay i try it

Update: So i tried it, but when i attach the trailer with bind, after i attach the trailer is immediately get deattached, after attached and again deattach and this is go loop.

How can i fix this issue?

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

 

Link to comment
16 hours ago, Syntrax# said:

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

 

is this client or server side?

Link to comment
6 hours ago, SphinxDev said:

Update: I try it but i got error.

debugscript 3 says:

ERROR: Loading script failed: an_trailersystem\server.lua:94: 'end' expected (to close 'function' at line 82) near '<eof>'

Might help if you paste your code here, can't see what you did with the code itself.

seeing the error code i see that you are missing an end within function detachTrailer

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
end
addEventHandler("onTrailerAttach", getRootElement(), detachTrailer)

 

Edited by Syntrax#
Link to comment
On 22/04/2022 at 00:16, Syntrax# said:

Might help if you paste your code here, can't see what you did with the code itself.

seeing the error code i see that you are missing an end within function detachTrailer

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
end
addEventHandler("onTrailerAttach", getRootElement(), detachTrailer)

 

I tried it, but i got error, but the trailer is got created.

debugscript says:

ERROR: an_trailersystem\server.lua:77: table index is nil

server side:
 

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(player,commandName)
	local vehicle = getPedOccupiedVehicle ( player )
  	if vehicle then
		local x,y,z = getElementPosition( vehicle )
		local rx,ry,rz = getElementRotation ( vehicle )
		local trailer = getNearestTrailer(player, 2000)
		if trailer then
			attachTrailerToVehicle ( vehicle, trailer )
			outputChatBox("Pótkocsit sikeresen felcsatoltad", player, 0, 255, 0)
		else
			outputChatBox("Nem található pótkocsi a közeledben!", player, 255, 0, 0)
		end
	else
		outputChatBox("Járműben kell ülnöd ehez!", player, 255, 0, 0)
	end
end

function unhookTrailer(player, key, keyState)
	local theVehicle = getPedOccupiedVehicle ( player )
	if theVehicle then
		local x,y,z = getElementPosition( theVehicle )
		local rx,ry,rz = getElementRotation ( theVehicle )
		local trailer = getNearestTrailer(player, 5000)
		if trailer then
			detachTrailerFromVehicle ( theVehicle, trailer )
			outputChatBox("Pótkocsit sikeresen lecsatoltad!", player, 0, 255, 0, false)
		else
			outputChatBox("Nem található pótkocsi a közeledben!", player, 255, 0, 0, false)
		end
	else
		outputChatBox("Járműben kell ülnöd ehez!", player, 255, 0, 0, false)
	end
end

addEventHandler("onResourceStart", resourceRoot, function()
    local players = getElementsByType("player")
    for i = 1, #players do
        local player = players[i]
        bindKey(player, "F3", "down", hookTrailer)
        bindKey(player, "F4", "down", unhookTrailer)
    end
end)

addEventHandler("onPlayerJoin", root, function()
	bindKey(player, "F3", "down", hookTrailer)
	bindKey(player, "F4", "down", unhookTrailer)
end)

-- just a table
trailers = {}

-- use this when creating the trailer
trailer = createVehicle ( 435, 2201.9609375, -2271.4736328125, 13.5625)
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
end
addEventHandler("onTrailerAttach", getRootElement(), detachTrailer)

function detachTrailer2(theVehicle, trailer)
    if (isElement(theVehicle) and isElement(trailer)) then
        detachTrailerFromVehicle(theVehicle, trailer)
    end
end

 

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