# Script Install

**Dependencies**

* One Sync Infinity
* [\[ox\_lib\]](https://github.com/overextended/ox_lib/releases)
* \[mk\_utils] (provided with script)

**Install Steps**

* Install Dependencies
* Download \[mk\_utils] from Keymaster (access provided with script purchase)
  * ![](https://51694285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSadY5GIZVZCGCeog04Gq%2Fuploads%2FU5gkLPTZnoumv95rS1Tj%2Fmk_utils_download.png?alt=media\&token=099d5518-04ee-48a1-ad6a-ac37a56b9d4a)
* add `mk_utils` to resource folder
* add `mk_vehiclekeys` to resource folder
* add `ensure mk_utils` into server.cfg (It must be added **BEFORE** mk\_vehiclekeys)
* add `ensure mk_vehiclekeys` into server.cfg
* Make sure `ox_lib` ace permissions are in `server.cfg`&#x20;

```lua
add_ace resource.ox_lib command.add_ace allow
add_ace resource.ox_lib command.remove_ace allow
add_ace resource.ox_lib command.add_principal allow
add_ace resource.ox_lib command.remove_principal allow
```

Your server.cfg should look similar to this when finished\\

<div align="left"><figure><img src="https://51694285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSadY5GIZVZCGCeog04Gq%2Fuploads%2FbgTvcZKDoJJ1jOwRIYxM%2Fcfg_keys.png?alt=media&#x26;token=2a68a7c7-8908-45cf-b2d0-4e9d5a864eb1" alt=""><figcaption></figcaption></figure></div>

{% tabs %}
{% tab title="qb-core" %}
goto file `qb-core/client/events.lua` and replace the event `QBCore:Command:SpawnVehicle` with the following

```lua
RegisterNetEvent('QBCore:Command:SpawnVehicle', function(vehName)
    local ped = PlayerPedId()
    local hash = joaat(vehName)
    local veh = GetVehiclePedIsUsing(ped)
    if not IsModelInCdimage(hash) then return end
    RequestModel(hash)
    while not HasModelLoaded(hash) do
        Wait(0)
    end

    if IsPedInAnyVehicle(ped) then
        SetEntityAsMissionEntity(veh, true, true)
        DeleteVehicle(veh)
    end

    local vehicle = CreateVehicle(hash, GetEntityCoords(ped), GetEntityHeading(ped), true, false)
    TaskWarpPedIntoVehicle(ped, vehicle, -1)
    SetVehicleFuelLevel(vehicle, 100.0)
    SetVehicleDirtLevel(vehicle, 0.0)
    SetModelAsNoLongerNeeded(hash)
    --TriggerEvent('vehiclekeys:client:SetOwner', QBCore.Functions.GetPlate(vehicle))
    exports.mk_vehiclekeys:AddKey(vehicle)
end)
```

In the same file replace the event `QBCore:Client:VehicleInfo` with the following

```lua
RegisterNetEvent('QBCore:Client:VehicleInfo', function(info)
    local plate = QBCore.Functions.GetPlate(info.vehicle)
    local hasKeys = true

    if GetResourceState('qb-vehiclekeys') == 'started' then
        hasKeys = exports['qb-vehiclekeys']:HasKeys(plate)
    elseif GetResourceState('mk_vehiclekeys') == 'started' then
        hasKeys = exports.mk_vehiclekeys:HasKey(vehicle)
    end

    local data = {
        vehicle = info.vehicle,
        seat = info.seat,
        name = info.modelName,
        plate = plate,
        driver = GetPedInVehicleSeat(info.vehicle, -1),
        inseat = GetPedInVehicleSeat(info.vehicle, info.seat),
        haskeys = hasKeys
    }

    TriggerEvent('QBCore:Client:' .. info.event .. 'Vehicle', data)
end)
```

{% endtab %}

{% tab title="qbx" %}
goto `qbx_core/config/server.lua` and replace the function `giveVehicleKeys` with the following

```lua
giveVehicleKeys = function(src, plate, vehicle)
    exports.mk_vehiclekeys:AddKey(vehicle, src)
end,
```

goto file `qbx_core/config/client.lua` and replace the function `hasKeys` with the following

```lua
hasKeys = function(plate, vehicle)
    return exports.mk_vehiclekeys:HasKey(vehicle)
end,
```

goto `qbx_core/server/vehicle-persistence.lua` and replace the event handler `entityRemoved` with the following. This will allow keys to be restored on a player vehicle if it is respawned by the vehicle persistence code of qbx.

```lua
AddEventHandler('entityRemoved', function(entity)
    if not Entity(entity).state.persisted then return end
    local keys, keyfobId, oldKeyfobs = Entity(entity).state.Keys, Entity(entity).state.MKFOB, Entity(entity).state.MKOLDFOB
    local sessionId = Entity(entity).state.sessionId
    local coords = GetEntityCoords(entity)
    local heading = GetEntityHeading(entity)
    local bucket = GetEntityRoutingBucket(entity)
    local passengers = getPedsInVehicleSeats(entity)
    local vehicleId = getVehicleId(entity)
    if not vehicleId then return end

    local playerVehicle = exports.qbx_vehicles:GetPlayerVehicle(vehicleId)

    if DoesEntityExist(entity) then
        Entity(entity).state:set('persisted', nil, true)
        DeleteVehicle(entity)
    end

    local _, veh = qbx.spawnVehicle({
        model = playerVehicle.props.model,
        spawnSource = vec4(coords.x, coords.y, coords.z, heading),
        bucket = bucket,
        props = playerVehicle.props
    })

    Entity(veh).state:set('sessionId', sessionId, true)
    Entity(veh).state:set('vehicleid', vehicleId, false)

    if keys and next(keys) then Entity(veh).state.Keys = keys end
    if keyfobId then Entity(veh).state.MKFOB = keyfobId end
    if oldKeyfobs and next(oldKeyfobs) then Entity(veh).state.MKOLDFOB = oldKeyfobs end

    for i = 1, #passengers do
        local passenger = passengers[i]
        SetPedIntoVehicle(passenger.ped, veh, passenger.seat)
    end
end)
```

{% endtab %}

{% tab title="esx" %}
goto `es_extended/server/modules/commands.lua` and replace the command `car` with the following

```lua
ESX.RegisterCommand(
    "car",
    "admin",
    function(xPlayer, args, showError)
        if not xPlayer then
            return showError("[^1ERROR^7] The xPlayer value is nil")
        end

        local playerPed = GetPlayerPed(xPlayer.source)
        local playerCoords = GetEntityCoords(playerPed)
        local playerHeading = GetEntityHeading(playerPed)
        local playerVehicle = GetVehiclePedIsIn(playerPed, false)

        if not args.car or type(args.car) ~= "string" then
            args.car = "adder"
        end

        if playerVehicle then
            DeleteEntity(playerVehicle)
        end

        if Config.AdminLogging then
            ESX.DiscordLogFields("UserActions", "Spawn Car /car Triggered!", "pink", {
                { name = "Player", value = xPlayer and xPlayer.name or "Server Console", inline = true },
                { name = "ID", value = xPlayer and xPlayer.source or "Unknown ID", inline = true },
                { name = "Vehicle", value = args.car, inline = true },
            })
        end

        local xRoutingBucket = GetPlayerRoutingBucket(xPlayer.source)

        ESX.OneSync.SpawnVehicle(args.car, playerCoords, playerHeading, upgrades, function(networkId)
            if networkId then
                local vehicle = NetworkGetEntityFromNetworkId(networkId)

                if xRoutingBucket ~= 0 then
                    SetEntityRoutingBucket(vehicle, xRoutingBucket)
                end

                for _ = 1, 100 do
                    Wait(0)
                    SetPedIntoVehicle(playerPed, vehicle, -1)

                    if GetVehiclePedIsIn(playerPed, false) == vehicle then
                        break
                    end
                end
                
                exports.mk_vehiclekeys:AddKey(vehicle, xPlayer.source)

                if GetVehiclePedIsIn(playerPed, false) ~= vehicle then
                    showError("[^1ERROR^7] The player could not be seated in the vehicle")
                end
            end
        end)
    end,
    false,
    {
        help = TranslateCap("command_car"),
        validate = false,
        arguments = {
            { name = "car", validate = false, help = TranslateCap("command_car_car"), type = "string" },
        },
    }
)
```

{% endtab %}
{% endtabs %}

[Setup Lockpick Items](https://mankind-scripts.gitbook.io/documentation/scripts/vehicle-keys/installation/lockpick-item-setup)
