Jump to content

[HELP] Container carrier job script


Recommended Posts

I got one script that when you press the pet, you start the job, but I don't know how to make it when someone presses the pet a vehicle comes up? I would be grateful if someone could help me script:

 

local screenX, screenY = guiGetScreenSize()

local pickupPos = {2466.32324, -2589.31519, 13.66070}
local pickupBlip = nil
local pickupMarker = nil

local dropBlip = nil
local dropMarker = nil

local jobPed = nil
local jobActive = false

local Roboto = nil

local dropPositions = {
    {1773.18201, -2050.24146, 13.56654},
    --{2491.90967, -2640.29883, 13.64844}
}

local _destroyElement = destroyElement

addEventHandler("onClientResourceStart", resourceRoot, function()
    jobPed = createPed(100, 2449.72021, -2591.47607, 13.65694, 270)
    setElementFrozen(jobPed, true)
end)

function startJob()
    outputChatBox("You started the job", 0, 255, 0)

    destroyElement(pickupMarker)
    pickupMarker = createMarker(pickupPos[1], pickupPos[2], pickupPos[3] - 3.7, "cylinder", 4, 255, 162, 0, 200)

    destroyElement(pickupBlip)
    pickupBlip = createBlip(pickupPos[1], pickupPos[2], pickupPos[3] - 3.7, 0, 2, 255, 162, 0)

    Roboto = dxCreateFont("Roboto.ttf", 12)

    addEventHandler("onClientRender", root, renderMarkerInfo)
    addEventHandler("onClientMarkerHit", root, markerHandler)

    
end

function stopJob()
    outputChatBox("You finished the job", 0, 255, 0)

    removeEventHandler("onClientRender", root, renderMarkerInfo)
    removeEventHandler("onClientMarkerHit", root, markerHandler)

    destroyElement(pickupMarker)
    destroyElement(pickupBlip)
    destroyElement(dropMarker)
    destroyElement(dropBlip)
    destroyElement(Roboto)
end

addEventHandler("onClientClick", root, function(button, state, absoluteX, absoluteY, worldX, worldY, worldZ, clickedElement)
    if button == "left" and state == "down" then
        if clickedElement == jobPed then
            local x, y, z = getElementPosition(clickedElement)
            local distance = getDistanceBetweenPoints3D(x, y, z, getElementPosition(localPlayer))

            if distance < 5 then
                if jobActive then
                    stopJob()
                    jobActive = false
                else    
                    startJob()
                    jobActive = true
                end
            end
        end
    end
end)

local startAnim = false

function markerHandler(player, matchDim)
    if player == localPlayer and matchDim then
        if source == pickupMarker then
            local vehicle = getPedOccupiedVehicle(localPlayer)

            if isElement(vehicle) then
                tempContainer = createObject(2935, 0, 0, 0)
                setObjectScale(tempContainer, 0.925)
                attachElements(tempContainer, vehicle, 0, -1.4, 1.1)
                setElementAlpha(tempContainer, 0)
                toggleAllControls(false)
                setElementFrozen(vehicle, true)

                startAnim = true
                initAnimation("bgAlpha", true, {0, 0, 0}, {1, 0, 0}, 1000, "Linear", function()
                    triggerServerEvent("attachContainerToVehicle", localPlayer)

                    initAnimation("bgAlpha", true, {1, 0, 0}, {0, 0, 0}, 1000, "Linear", function()
                        toggleAllControls(true)
                        startAnim = false
                        setElementFrozen(vehicle, false)
                    end)
                end)
            end
        elseif source == dropMarker then
            local vehicle = getPedOccupiedVehicle(localPlayer)

            if isElement(vehicle) then
                setElementFrozen(vehicle, true)

                startAnim = true
                initAnimation("bgAlpha", true, {0, 0, 0}, {1, 0, 0}, 1000, "Linear", function()
                    triggerServerEvent("detachContainerFromVehicle", localPlayer)

                    initAnimation("bgAlpha", true, {1, 0, 0}, {0, 0, 0}, 1000, "Linear", function()
                        toggleAllControls(true)
                        startAnim = false
                        setElementFrozen(vehicle, false)
                    end)
                end)
                destroyElement(dropMarker)
                destroyElement(dropBlip)
            end
        end
    end
end

function generateDropPoint()
    destroyElement(dropMarker)
    destroyElement(dropBlip)

    local rand = math.random(1, #dropPositions)
    local x, y, z = unpack(dropPositions[rand])

    dropBlip = createBlip(x, y, z - 3.7, 0, 2, 50, 168, 82)
    dropMarker = createMarker(x, y, z - 3.7, "cylinder", 4, 50, 168, 82, 200)
end
addEvent("generateDropPoint", true)
addEventHandler("generateDropPoint", root, generateDropPoint)

function renderMarkerInfo()
    if isElement(pickupMarker) then
        local mX, mY, mZ = getElementPosition(pickupMarker)
        local distance = getDistanceBetweenPoints3D(mX, mY, mZ, getElementPosition(localPlayer))
        local maxDistance = 30

        if distance < maxDistance then
            local scale = 1 - distance / maxDistance
            local w, h = 210, 50
            local x, y = getScreenFromWorldPosition(mX, mY, mZ + 4)

            if x and y then
                w, h = w * scale, h * scale
                x, y = x - w / 2, y - h / 2 

                dxDrawBorderedRectangle(x, y, w, h, tocolor(0, 0, 0, 150), tocolor(0, 0, 0, 200), 2)
                dxDrawText("Container pickup point", x, y, x + w, y + h, tocolor(255, 255, 255), 1 * scale, Roboto, "center", "center")
            end
        end
    end

    if isElement(dropMarker) then
        local mX, mY, mZ = getElementPosition(dropMarker)
        local distance = getDistanceBetweenPoints3D(mX, mY, mZ, getElementPosition(localPlayer))
        local maxDistance = 30

        if distance < maxDistance then
            local scale = 1 - distance / maxDistance
            local w, h = 210, 50
            local x, y = getScreenFromWorldPosition(mX, mY, mZ + 4)

            if x and y then
                w, h = w * scale, h * scale
                x, y = x - w / 2, y - h / 2 

                dxDrawBorderedRectangle(x, y, w, h, tocolor(0, 0, 0, 150), tocolor(0, 0, 0, 200), 2)
                dxDrawText("Container drop off point", x, y, x + w, y + h, tocolor(255, 255, 255), 1 * scale, Roboto, "center", "center")
            end
        end
    end
end

function destroyElement(element)
    if isElement(element) then
        return _destroyElement(element)    
    end
end

function dxDrawBorderedRectangle( x, y, width, height, color1, color2, _width, postGUI )
    local _width = _width or 1
    dxDrawRectangle ( x+1, y+1, width-1, height-1, color1, postGUI )
    dxDrawLine ( x, y, x+width, y, color2, _width, postGUI ) -- Top
    dxDrawLine ( x, y, x, y+height, color2, _width, postGUI ) -- Left
    dxDrawLine ( x, y+height, x+width, y+height, color2, _width, postGUI ) -- Bottom
    dxDrawLine ( x+width, y, x+width, y+height, color2, _width, postGUI ) -- Right
end

---------------------------------------------------------------------------------------------------------------------------------------------------

local animations = {}

addEventHandler("onClientRender", root, function()
    for k, v in pairs(animations) do
        if not v.completed then
            local currentTick = getTickCount()
            local elapsedTick = currentTick - v.startTick
            local duration = v.endTick - v.startTick
            local progress = elapsedTick / duration

            v.currentValue[1], v.currentValue[2], v.currentValue[3] = interpolateBetween(
                v.startValue[1], v.startValue[2], v.startValue[3], 
                v.endValue[1], v.endValue[2], v.endValue[3], 
                progress, 
                v.easingType or "Linear"
            )

            if progress >= 1 then
                v.completed = true

                if v.completeFunction then
                    v.completeFunction(unpack(v.functionArgs))
                end
            end
        end
    end
    
    if startAnim then
    dxDrawRectangle(0, 0, screenX, screenY, tocolor(0, 0, 0, 255 * getAnimationValue("bgAlpha")[1]))
    end
end)

function initAnimation(id, storeVal, startVal, endVal, time, easing, compFunction, args)
    if not storeVal then
        animations[id] = {}
    end

    if not animations[id] then
        animations[id] = {}
    end

    animations[id].startValue = startVal
    animations[id].endValue = endVal
    animations[id].startTick = getTickCount()
    animations[id].endTick = animations[id].startTick + (time or 3000)
    animations[id].easingType = easing
    animations[id].completeFunction = compFunction
    animations[id].functionArgs = args or {}

    animations[id].currentValue = storeVal and animations[id].currentValue or {0, 0, 0}
    animations[id].completed = false
end

function getAnimationValue(id)
    if animations[id] then
        return animations[id].currentValue
    end

    return {0, 0, 0}
end

function setAnimationValue(id, val)
    animations[id].currentValue = val 
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...