Jump to content

LIMIT MULTI TRAILER SPAWN


Recommended Posts

LIMIT MULTI-TRAILER SPAWN, AND MAKE PREVIOUS TRAILERS DISAPPEAR.
I need this script to allow the player to do the command only once, and when trying a next command, the previous one will disappear.
follow my script, and if possible do it and briefly explain what was done, I'm a layman in programming!

 

local trailers = {}
local hooktruck = {}

addCommandHandler ("trailers",
    function (thePlayer, cmd, trailernum)
        if not trailernum then outputChatBox ("* Syntax is: /trailers number", thePlayer, 255, 0, 0) return end
        local num = tonumber (trailernum)
         if type (num) ~= "number" then outputChatBox ("* Invalid trailer number provided", thePlayer, 255, 0, 0) return end
        
        if not trailers[thePlayer] then trailers[thePlayer] = {} end
        if not hooktruck[thePlayer] then hooktruck[thePlayer] = {} end
        
        local myVeh = getPedOccupiedVehicle (thePlayer)
        for i=1, num do
            -- if it's the first trailer, attach it to the original truck
            if i == 1 then
                trailers[thePlayer][1] = createVehicle (591, 0, 0, 10)
                setElementRotation (trailers[thePlayer][1], getElementRotation (myVeh))
                setTimer (attachTrailerToVehicle, 200, 1, myVeh, trailers[thePlayer][1])
                
            -- if it's not then
            else
                -- Here we are creating invisible trucks and attaching them to the back of each trailer so we can have multiple trailers
                hooktruck[thePlayer][i-1] = createVehicle (514, 0, 750, 1500)
                setElementAlpha (hooktruck[thePlayer][i-1], 0)
                setElementCollisionsEnabled (hooktruck[thePlayer][i-1], false)
                setVehicleDamageProof (hooktruck[thePlayer][i-1], true)
                setElementRotation (hooktruck[thePlayer][i-1], getElementRotation (myVeh))
                addEventHandler ("onVehicleStartEnter", hooktruck[thePlayer][i-1], cancelRoadtrainEntering)
                setTimer (attachElements, 300 * i, 1, hooktruck[thePlayer][i-1], trailers[thePlayer][i-1], 0, -0.1, 0)
                
                -- Let's create our trailer for attaching to invisible trucks
                trailers[thePlayer] = createVehicle (591, 2000, 2000, 500)
                setElementRotation (trailers[thePlayer], getElementRotation (myVeh))
                setTimer (attachTrailerToVehicle, 400 * i, 1, hooktruck[thePlayer][i-1], trailers[thePlayer])
            end
        end
    end
)

function cancelRoadtrainEntering ()
    cancelEvent()
end
 

 

Link to comment
On 09/07/2022 at 16:59, WillChris said:

LIMIT MULTI-TRAILER SPAWN, AND MAKE PREVIOUS TRAILERS DISAPPEAR.
I need this script to allow the player to do the command only once, and when trying a next command, the previous one will disappear.
follow my script, and if possible do it and briefly explain what was done, I'm a layman in programming!

 

local trailers = {}
local hooktruck = {}

addCommandHandler ("trailers",
    function (thePlayer, cmd, trailernum)
        if not trailernum then outputChatBox ("* Syntax is: /trailers number", thePlayer, 255, 0, 0) return end
        local num = tonumber (trailernum)
         if type (num) ~= "number" then outputChatBox ("* Invalid trailer number provided", thePlayer, 255, 0, 0) return end
        
        if not trailers[thePlayer] then trailers[thePlayer] = {} end
        if not hooktruck[thePlayer] then hooktruck[thePlayer] = {} end
        
        local myVeh = getPedOccupiedVehicle (thePlayer)
        for i=1, num do
            -- if it's the first trailer, attach it to the original truck
            if i == 1 then
                trailers[thePlayer][1] = createVehicle (591, 0, 0, 10)
                setElementRotation (trailers[thePlayer][1], getElementRotation (myVeh))
                setTimer (attachTrailerToVehicle, 200, 1, myVeh, trailers[thePlayer][1])
                
            -- if it's not then
            else
                -- Here we are creating invisible trucks and attaching them to the back of each trailer so we can have multiple trailers
                hooktruck[thePlayer][i-1] = createVehicle (514, 0, 750, 1500)
                setElementAlpha (hooktruck[thePlayer][i-1], 0)
                setElementCollisionsEnabled (hooktruck[thePlayer][i-1], false)
                setVehicleDamageProof (hooktruck[thePlayer][i-1], true)
                setElementRotation (hooktruck[thePlayer][i-1], getElementRotation (myVeh))
                addEventHandler ("onVehicleStartEnter", hooktruck[thePlayer][i-1], cancelRoadtrainEntering)
                setTimer (attachElements, 300 * i, 1, hooktruck[thePlayer][i-1], trailers[thePlayer][i-1], 0, -0.1, 0)
                
                -- Let's create our trailer for attaching to invisible trucks
                trailers[thePlayer] = createVehicle (591, 2000, 2000, 500)
                setElementRotation (trailers[thePlayer], getElementRotation (myVeh))
                setTimer (attachTrailerToVehicle, 400 * i, 1, hooktruck[thePlayer][i-1], trailers[thePlayer])
            end
        end
    end
)

function cancelRoadtrainEntering ()
    cancelEvent()
end
 

 

local trailers = {}
local hooktruck = {}

addCommandHandler ("trailers",
    function (thePlayer, cmd, trailernum)
        if not trailernum or not tonumber (trailernum) then outputChatBox ("* Syntax is: /trailers number", thePlayer, 255, 0, 0) return end
        local num = tonumber (trailernum)

        if num < 1 or num > 15 then
            outputChatBox ("* You can create trailer in the range from 1 to 15", thePlayer, 255, 0, 0) 
            return
        end
        
        if trailers[thePlayer] then
            for k, trailer in pairs (trailers[thePlayer]) do
                if isElement (trailer) then
                    destroyElement(trailer)
                end
                trailers[thePlayer][k] = nil
            end
            trailers[thePlayer] = {} 
        else
            trailers[thePlayer] = {} 
        end

        if hooktruck[thePlayer] then 
            if isElement (hooktruck[thePlayer]) then
                destroyElement(hooktruck[thePlayer])
            end
            hooktruck[thePlayer] = nil
        end
        
        local myVeh = getPedOccupiedVehicle (thePlayer)
        if not myVeh then
            outputChatBox ("* You need seat in vehicle", thePlayer, 255, 0, 0)
            return
        end

        local pos = Vector3(getElementPosition(myVeh))

        for i=1, num do
            if i == 1 then
                hooktruck[thePlayer] = createVehicle (591, pos)
                setTimer (attachTrailerToVehicle, 200, 1, myVeh, hooktruck[thePlayer][1])
            else
                trailers[thePlayer][i-1] = createVehicle (514, 0, 0, 10)
                local parent = (i == 2) and hooktruck[thePlayer] or trailers[thePlayer][i-2]
                setTimer (attachTrailerToVehicle, 200, 1, parent, trailers[thePlayer][i-1])
            end
        end
        collectgarbage()
    end
)

Try this code

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