Tutorials

How to lock/unlock vehicle with third party script ?

How to implement fivecode_carkeys with your vehicleshop so your players receive vehicle keys upon purchase of a new vehicle ?

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.

How to make your vehicle drivable after lockpicking it while Config.NeedKeysToStartVehicles is true

How to create vehicle_keys table in your database

1.

Toggle vehicle lock/unlock export:

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

Toggle vehicle lock/unlock export - EXAMPLE:

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

Give car keys export:

Client side:

---@param vehicle integer
---@param plate string
---@param isSociety boolean [default false]
---@param giveKey boolean [true - give key, false - remove key]
exports.fivecode_carkeys:GiveKey(vehicle, isSociety, giveKey)

Give car keys export - EXAMPLE:

Script used for this demonstration: https://github.com/esx-framework/esx_vehicleshop

[esx_vehicleshop/server/main.lua] line - 152
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)
		
		    exports.fivecode_carkeys:GiveKey(source, {vehicle = vehicle, plate = plate, model = model}, false, true)
		
		end)
		cb(true)
	    end)
	else
        cb(false)
    end
end)

3.

Remove/give player keys when storing/taking out vehicle from garage:

---@param vehicle integer
---@param hasStored boolean [true - Store(Take keys), false - Take out(Give Keys)]
exports.fivecode_carkeys:StoreVehicleKey(vehicle, hasStored)

4.

Make vehicle drivable after lockpicking it:

---@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)

5.

Create vehicle_keys table in your database:

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.

Last updated