Salta al contenuto

Car Key Script | Fivem

-- Lock/Unlock vehicle with key Citizen.CreateThread(function() while true do Citizen.Wait(0) if IsControlJustPressed(0, 38) and IsControlPressed(0, 21) then -- L key (change in config) local ped = PlayerPedId() local vehicle = GetVehiclePedIsIn(ped, false) if vehicle == 0 then -- Not in vehicle, find nearest vehicle within distance local pos = GetEntityCoords(ped) local vehicle = GetClosestVehicle(pos.x, pos.y, pos.z, Config.InteractDistance, 0, 70) if vehicle ~= 0 then local plate = GetVehicleNumberPlateText(vehicle) lib.callback.await('carkeys:hasKey', false, plate) if hasKey then if GetVehicleDoorLockStatus(vehicle) == 2 then SetVehicleDoorsLocked(vehicle, 1) vehicleLocked = false Config.Notify('Vehicle unlocked', 'success') else SetVehicleDoorsLocked(vehicle, 2) vehicleLocked = true Config.Notify('Vehicle locked', 'info') end else Config.Notify('You don't have the key for this vehicle', 'error') end end else -- Inside vehicle, lock/unlock current vehicle local plate = GetVehicleNumberPlateText(vehicle) lib.callback.await('carkeys:hasKey', false, plate) if hasKey then if GetVehicleDoorLockStatus(vehicle) == 2 then SetVehicleDoorsLocked(vehicle, 1) vehicleLocked = false Config.Notify('Vehicle unlocked', 'success') else SetVehicleDoorsLocked(vehicle, 2) vehicleLocked = true Config.Notify('Vehicle locked', 'info') end end end end end end)

-- Distance to interact with vehicle (for remote lock/unlock) Config.InteractDistance = 10.0 local Framework = nil if Config.Framework == 'esx' then TriggerEvent('esx:getSharedObject', function(obj) Framework = obj end) elseif Config.Framework == 'qb' then Framework = exports['qb-core']:GetCoreObject() end

-- Give a car key to a player RegisterNetEvent('carkeys:giveKey') AddEventHandler('carkeys:giveKey', function(plate, targetId) local src = source local xPlayer = Framework.GetPlayerFromId(src) local targetPlayer = Framework.GetPlayerFromId(targetId)

if targetPlayer then local keyItem = string.format('%s_%s', Config.KeyItem, plate) targetPlayer.addInventoryItem(keyItem, 1) TriggerClientEvent('carkeys:keyReceived', targetId, plate) Config.Notify('Key given for vehicle: ' .. plate, 'success') end end) fivem car key script

client_scripts { 'config.lua', 'client.lua' }

-- Key item name (create this in your database/items) Config.KeyItem = 'car_key'

server_scripts { 'config.lua', 'server.lua' } -- Lock/Unlock vehicle with key Citizen

-- Toggle engine on/off (only if you have key) Citizen.CreateThread(function() while true do Citizen.Wait(0) if IsControlJustPressed(0, 303) then -- U key (change in config) local ped = PlayerPedId() if IsPedInAnyVehicle(ped, false) then local vehicle = GetVehiclePedIsIn(ped, false) local plate = GetVehicleNumberPlateText(vehicle) lib.callback.await('carkeys:hasKey', false, plate) if hasKey then engineOn = not engineOn SetVehicleEngineOn(vehicle, engineOn, false, true) if engineOn then Config.Notify('Engine started', 'success') else Config.Notify('Engine turned off', 'info') end else Config.Notify('You don't have the key!', 'error') end end end end end)

-- Engine start keybind (optional) Config.EngineKeybind = 'U'

-- Check if player has key for specific plate lib.callback.register('carkeys:hasKey', function(source, plate) local xPlayer = Framework.GetPlayerFromId(source) local keyItem = string.format('%s_%s', Config.KeyItem, plate) local count = xPlayer.getInventoryItem(keyItem).count return count > 0 end) local Framework = nil local currentVehicle = nil local engineOn = false local vehicleLocked = true local ownedVehicles = {} -- store plate -> hasKey -- Load framework Citizen.CreateThread(function() if Config.Framework == 'esx' then TriggerEvent('esx:getSharedObject', function(obj) Framework = obj end) elseif Config.Framework == 'qb' then Framework = exports['qb-core']:GetCoreObject() end end) 38) and IsControlPressed(0

-- Remove key from player (when vehicle is sold/destroyed) RegisterNetEvent('carkeys:removeKey') AddEventHandler('carkeys:removeKey', function(plate) local src = source local xPlayer = Framework.GetPlayerFromId(src) local keyItem = string.format('%s_%s', Config.KeyItem, plate) xPlayer.removeInventoryItem(keyItem, 1) end)

-- Block vehicle start without key (prevent hotwiring) Citizen.CreateThread(function() while true do Citizen.Wait(100) local ped = PlayerPedId() if IsPedInAnyVehicle(ped, false) then local vehicle = GetVehiclePedIsIn(ped, false) local plate = GetVehicleNumberPlateText(vehicle) lib.callback.await('carkeys:hasKey', false, plate) if not hasKey and GetPedInVehicleSeat(vehicle, -1) == ped then SetVehicleEngineOn(vehicle, false, false, true) Citizen.Wait(500) end end end end) Add this item to your items table:

-- Get vehicle from player's last entered Citizen.CreateThread(function() while true do Citizen.Wait(1000) local ped = PlayerPedId() if IsPedInAnyVehicle(ped, false) then local vehicle = GetVehiclePedIsIn(ped, false) if vehicle ~= currentVehicle then currentVehicle = vehicle local plate = GetVehicleNumberPlateText(vehicle) -- Check if player has key lib.callback.await('carkeys:hasKey', false, plate) hasKey = result if not hasKey then Config.Notify('You do not have the key for this vehicle!', 'error') -- Eject player from driver seat if GetPedInVehicleSeat(vehicle, -1) == ped then TaskLeaveVehicle(ped, vehicle, 0) end else vehicleLocked = false Config.Notify('You have the key for this vehicle', 'success') end end else currentVehicle = nil engineOn = false end end end)

carkeys/ ├── fxmanifest.lua ├── client.lua ├── server.lua ├── config.lua └── html/ (optional – for UI notifications) 🔧 1. fxmanifest.lua fx_version 'cerulean' game 'gta5' author 'YourName' description 'Car Key System for FiveM' version '1.0.0'

dependencies { 'es_extended' -- or 'qb-core' depending on your framework } Config = {} -- Framework (esx or qb) Config.Framework = 'esx' -- change to 'qb' if using QBCore