# Tutorials

[How to lock/unlock vehicle with third party script ?](#id-1)

[How to implement fivecode\_carkeys with your vehicleshop so your players receive vehicle keys upon purchase of a new vehicle ?](#id-2)

[How to implement fivecode\_carkeys with your garage system so that when you store your vehicle, the keys will be removed from you, and when you take it out, they will be given to you.](#id-3)

[How to make your vehicle drivable after lockpicking it while Config.NeedKeysToStartVehicles is true](#id-4)

[How to create _vehicle\_keys_ table in your database](#id-5)

#### 1.

{% hint style="info" %}
**Toggle vehicle lock/unlock export:**
{% endhint %}

```lua
---@param vehicle integer
---@param status Optional[integer] (1 || 2)
exports.fivecode_carkeys:ToggleLock(vehicle, status)
```

{% hint style="info" %}
**Toggle vehicle lock/unlock export - EXAMPLE:**
{% endhint %}

```lua
-- For example in ox_target
exports.ox_target:addGlobalVehicle({
    {
        name = 'ox_target:driverF',
        icon = 'fa-solid fa-car-side',
        label = 'Toggle Lock Vehicle',
        bones = { 'bodyshell' },
        canInteract = function(entity, distance, coords, name)

            local boneId = GetEntityBoneIndexByName(entity, 'door_dside_f')

            if boneId ~= -1 then
                return #(coords - GetWorldPositionOfEntityBone(entity, boneId)) < 0.5 or #(coords - GetWorldPositionOfEntityBone(entity, GetEntityBoneIndexByName(entity, 'seat_dside_f'))) < 0.72
            end
        end,
        onSelect = function(data)
            exports.fivecode_carkeys:ToggleLock(data.entity)
        end
    }
})
```

#### 2.

{% hint style="info" %}
**Give car keys export:**
{% endhint %}

{% tabs %}
{% tab title="Client" %}
Client side:

```lua
---@param vehicle integer
---@param isSociety boolean [default false]
---@param giveKey boolean [true - give key, false - remove key]
exports.fivecode_carkeys:GiveKey(vehicle, isSociety, giveKey)
```
{% endtab %}

{% tab title="Server" %}
Server side:

```lua
---@param source integer
---@param vehicle integer
---@param plate string
---@param model integer
---@param isSociety boolean [default false]
---@param giveKey boolean [true - give key, false - remove key]
exports.fivecode_carkeys:GiveKey(source, {vehicle = vehicle, plate = plate, model = model}, isSociety, giveKey)
```
{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Give car keys export - EXAMPLE:**
{% endhint %}

{% tabs %}
{% tab title="ESX" %}
Script used for this demonstration:\
[https://github.com/esx-framework/esx\_vehicleshop](https://github.com/esx-framework/esx_vehicleshop)

<pre class="language-lua" data-title="[esx_vehicleshop/server/main.lua] line - 152"><code class="lang-lua">ESX.RegisterServerCallback('esx_vehicleshop:buyVehicle', function(source, cb, model, plate)
	local xPlayer = ESX.GetPlayerFromId(source)
	local modelPrice = getVehicleFromModel(model).price

	if modelPrice and xPlayer.getMoney() >= modelPrice then
		xPlayer.removeMoney(modelPrice, "Vehicle Purchase")

		MySQL.insert('INSERT INTO owned_vehicles (owner, plate, vehicle) VALUES (?, ?, ?)', {xPlayer.identifier, plate, json.encode({model = joaat(model), plate = plate})
		}, function(rowsChanged)
		    xPlayer.showNotification(TranslateCap('vehicle_belongs', plate))
		    ESX.OneSync.SpawnVehicle(joaat(model), Config.Zones.ShopOutside.Pos, Config.Zones.ShopOutside.Heading,{plate = plate}, function(vehicle)
		    Wait(100)
		    local vehicle = NetworkGetEntityFromNetworkId(vehicle)
		    Wait(300)
		    TaskWarpPedIntoVehicle(GetPlayerPed(source), vehicle, -1)
		
<strong>		    exports.fivecode_carkeys:GiveKey(source, {vehicle = vehicle, plate = plate, model = model}, false, true)
</strong>		
		end)
		cb(true)
	    end)
	else
        cb(false)
    end
end)
</code></pre>
{% endtab %}

{% tab title="QBCORE" %}
Script used for this demonstration:\
[https://github.com/qbcore-framework/qb-vehicleshop](https://github.com/qbcore-framework/qb-vehicleshop)

<pre class="language-lua" data-title="[qb-vehicleshop/client.lua] line - 649"><code class="lang-lua">RegisterNetEvent('qb-vehicleshop:client:buyShowroomVehicle', function(vehicle, plate)
    tempShop = insideShop
    QBCore.Functions.TriggerCallback('QBCore:Server:SpawnVehicle', function(netId)
        local veh = NetToVeh(netId)
        exports['LegacyFuel']:SetFuel(veh, 100)
        SetVehicleNumberPlateText(veh, plate)
        SetEntityHeading(veh, Config.Shops[tempShop]["VehicleSpawn"].w)
        TriggerEvent("vehiclekeys:client:SetOwner", QBCore.Functions.GetPlate(veh))
        TriggerServerEvent("qb-vehicletuning:server:SaveVehicleProps", QBCore.Functions.GetVehicleProperties(veh))
        
<strong>        exports.fivecode_carkeys:GiveKey(veh, false, true)
</strong>
    end, vehicle, Config.Shops[tempShop]["VehicleSpawn"], true)
end)
</code></pre>
{% endtab %}
{% endtabs %}

#### 3.

{% hint style="info" %}
Remove/give player keys when storing/taking out vehicle from garage:
{% endhint %}

{% tabs %}
{% tab title="Client" %}
```lua
---@param vehicle integer
---@param hasStored boolean [true - Store(Take keys), false - Take out(Give Keys)]
exports.fivecode_carkeys:StoreVehicleKey(vehicle, hasStored)
```
{% endtab %}

{% tab title="Server" %}
```lua
---@param source integer
---@param vehicle integer
---@param model integer
---@param hasStored boolean [true - Store(Take keys), false - Take out(Give Keys)]
exports.fivecode_carkeys:StoreVehicleKey(source, {vehicle = vehicle, model = model}, hasStored)
```
{% endtab %}
{% endtabs %}

#### 4.

{% hint style="info" %}
Make vehicle drivable after lockpicking it:
{% endhint %}

{% tabs %}
{% tab title="Client" %}
```lua
---@param vehicle integer
---@param status boolean [true - Can drive without a key(lockpicked), false - Can't drive without a key(not lockpicked)]
exports.fivecode_carkeys:LockpickedVehicle(vehicle, status)
```
{% endtab %}

{% tab title="Server" %}
```lua
---@param vehicle integer
---@param status boolean [true - Can drive without a key(lockpicked), false - Can't drive without a key(not lockpicked)]
exports.fivecode_carkeys:LockpickedVehicle(vehicle, status)
```
{% endtab %}
{% endtabs %}

#### 5.

{% hint style="info" %}
**Create&#x20;**_**vehicle\_keys**_**&#x20;table in your database:**
{% endhint %}

```sql
CREATE TABLE IF NOT EXISTS `vehicle_keys` (
     `id` INT(11) NOT NULL AUTO_INCREMENT,
     `vehId` VARCHAR(50) NULL DEFAULT NULL COMMENT 'num or society (Universal key)' COLLATE 'utf8mb4_general_ci',
     `isActive` TINYINT(4) NOT NULL DEFAULT '1',
     `isStored` TINYINT(4) NOT NULL DEFAULT '0',
   PRIMARY KEY (`id`) USING BTREE
);
```

> If you need help with anything, do not hesitate and open a support ticket in our [discord](https://discord.gg/Yf7GrHDuUr).

