QB-Vehiclekeys

Would it be possible to add a npc vehicle lock generate randomly in qb-vehiclekeys for example qb-vehiclekeys by default locks every npc car, would i be able to add chances to it for say 30% unlocked chance and 70% locked chance or something similar to where some cars are locked and others arent?

1 Like

Yeah you can add a random chance using the math.random function. There’s a part of the code in qb-vehiclekeys/client/main.lua which checks for the config values of parked NPC cars being locked and driving NPC cars being locked. In these if statements, you could add a chance by doing something like this:

elseif Config.LockNPCDrivingCars then
    if math.random(1, 100) <= 70 then --70% chance
        TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(entering), 2)
        TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', plate)
    else --30% chance
        TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(entering), 1)
        TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', plate)

        --Make passengers flee
        local pedsInVehicle = GetPedsInVehicle(entering)
        for _, pedInVehicle in pairs(pedsInVehicle) do
            if pedInVehicle ~= GetPedInVehicleSeat(entering, -1) then
                MakePedFlee(pedInVehicle)
            end
        end
    end
else
--Continue code

Then with Config.LockNPCParkedCars:

if Config.LockNPCParkedCars then
    if math.random(1, 100) <= 70 then -- 70% chance
        TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(entering), 2)
    else -- 30% Chance
        TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(entering), 1)
    end
else
--Continue code
1 Like

Thank you so much im gonna try this right now!

so after adding this in i believe it just disabled carjacking and gave it a 100% unlock chance cause ive been trying cars for 5 minutes and all where unlocked and when i tried carjacking someone it didnt work at all

Are both config options set to true? And are you sure you added it to the code correctly?

i added all of that code into the client main.lua correct? also i added the config lines like so Config.LockNPCParkedCars = true
Config.LockNPCDrivingCars = true

Not specifically all of the code because I included some lines from the already existing code just to show where it gets implemented in relation it.

yeah besides that i didnt copy the duplicate code

Type ensure qb-vehiclekeys into your console to see if it starts up with any errors. If there is errors, you will see them in your F8 console

ok to be sure ill add what i added here and please correct me if its incorrect

---- Variables ----

local QBCore = exports[‘qb-core’]:GetCoreObject()
local KeysList = {}

local isTakingKeys = true
local isCarjacking = true
local canCarjack = true
local AlertSend = false
local lastPickedVehicle = nil
local usingAdvanced = true
local IsHotwiring = false


---- Threads ----

CreateThread(function()
while true do
local sleep = 1000
if LocalPlayer.state.isLoggedIn then
sleep = 100

        local ped = PlayerPedId()
        local entering = GetVehiclePedIsTryingToEnter(ped)
        local carIsImmune = false
        if entering ~= 0 and not isBlacklistedVehicle(entering) then
            sleep = 2000
            local plate = QBCore.Functions.GetPlate(entering)

            local driver = GetPedInVehicleSeat(entering, -1)
            for _, veh in ipairs(Config.ImmuneVehicles) do
                if GetEntityModel(entering) == GetHashKey(veh) then
                    carIsImmune = true
                end
            end
            if driver ~= 0 and not IsPedAPlayer(driver) and not HasKeys(plate) and not carIsImmune then
                if IsEntityDead(driver) then
                    if not isTakingKeys then
                        isTakingKeys = true
                        SetVehicleDoorsLocked(entering, 1)
                        QBCore.Functions.Progressbar("steal_keys", "Taking keys from body...", 2500, false, false, {
                            disableCarMovement = true,
                            disableMouse = false,
                            disableCombat = true
                        }, {}, {}, {}, function() -- Done
                            TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', plate)
                            isTakingKeys = false
                        end, function()
                            isTakingKeys = false
                        end)
                    end
                elseif Config.LockNPCDrivingCars then
                    TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(entering), 2)
                else
                    TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(entering), 1)
                    TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', plate)
                
                          
                    --Make passengers flee
                    local pedsInVehicle = GetPedsInVehicle(entering)
                    for _, pedInVehicle in pairs(pedsInVehicle) do
                        if pedInVehicle ~= GetPedInVehicleSeat(entering, -1) then
                            MakePedFlee(pedInVehicle)

-- Parked car logic
elseif driver == 0 and entering ~= lastPickedVehicle and not HasKeys(plate) and not isTakingKeys then
    if Config.LockNPCParkedCars then
        TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(entering), 2)
    else
        TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(entering), 1)
    end
end

end

elseif Config.LockNPCDrivingCars then
if math.random(1, 100) <= 70 then --70% chance
TriggerServerEvent(‘qb-vehiclekeys:server:setVehLockState’, NetworkGetNetworkIdFromEntity(entering), 2)
else --30% chance
TriggerServerEvent(‘qb-vehiclekeys:server:setVehLockState’, NetworkGetNetworkIdFromEntity(entering), 1)
TriggerServerEvent(‘qb-vehiclekeys:server:AcquireVehicleKeys’, plate)

        --Make passengers flee
        local pedsInVehicle = GetPedsInVehicle(entering)
            for _, pedInVehicle in pairs(pedsInVehicle) do
                if pedInVehicle ~= GetPedInVehicleSeat(entering, -1) then
                    MakePedFlee(pedInVehicle)
                end
            end
        end
    else

if Config.LockNPCParkedCars then
if math.random(1, 100) <= 70 then – 70% chance
TriggerServerEvent(‘qb-vehiclekeys:server:setVehLockState’, NetworkGetNetworkIdFromEntity(entering), 2)
else – 30% Chance
TriggerServerEvent(‘qb-vehiclekeys:server:setVehLockState’, NetworkGetNetworkIdFromEntity(entering), 1)
end
else

– Hotwiring while in vehicle, also keeps engine off for vehicles you don’t own keys to
if IsPedInAnyVehicle(ped, false) and not IsHotwiring then
sleep = 1000
local vehicle = GetVehiclePedIsIn(ped)
local plate = QBCore.Functions.GetPlate(vehicle)
if GetPedInVehicleSeat(vehicle, -1) == PlayerPedId() and not HasKeys(plate) and not isBlacklistedVehicle(vehicle) then
sleep = 5
local vehiclePos = GetOffsetFromEntityInWorldCoords(vehicle, 0.0, 1.0, 0.5)
DrawText3D(vehiclePos.x, vehiclePos.y, vehiclePos.z, “~g~[H]~w~ - Search for Keys”)
SetVehicleEngineOn(vehicle, false, false, true)
if IsControlJustPressed(0, 74) then
Hotwire(vehicle, plate)

     end
end

end

i have none at all

From what I can tell based off of the code you pasted, it didn’t look correctly implemented. Try my fork: qb-vehiclekeys/main.lua at main · Qwerty1Verified/qb-vehiclekeys · GitHub

1 Like

Just used the fork now i got an error in my F8 console

Do you have a locale set in your fxmanifest? If so, have you edited this locale?

1 Like

in my fxmanifest for qb-vehiclekeys i have

fx_version ‘cerulean’

game ‘gta5’

description ‘QB-VehicleKeys’

version ‘1.0.0’

shared_script ‘config.lua’

client_script ‘client/main.lua’

server_script ‘server/main.lua’

lua54 ‘yes’

so no i do not

i just realized im missing the entire locales folder for qb-vehiclekeys

Yeah your manifest looks outdated, implement those and let me know if the fork works.

just updated my qb-vehiclekeys gonna add it right now

it works!! thank you so much for helping me and replying ive wanted to implement this for a while now but didnt know exactly how to considering im not very advanced with coding as ive started learning about it in the last 3 months you saved me big time thank you have a great day

one more thing if you dont mind if i wanted to change the chances of the car being unlocked/locked how could i do so?