Skip to content

Commit 3d226d9

Browse files
Improve VehicleInfo Event Handling
Improve VehicleInfo Event Handling with Fallbacks and Optional Key Check Description: This PR enhances the QBCore:Client:VehicleInfo event logic for improved compatibility and error safety: Added fallback method to retrieve the vehicle plate using QBCore.Functions.GetPlate() if not directly passed; Added support for checking if the player has vehicle keys only when qb-vehiclekeys is active; Defaulted hasKeys to true to avoid issues when the key system is not used; Cleaned up the logic for packaging and passing vehicle information. These changes make the system more robust and prevent crashes on servers not using qb-vehiclekeys.
1 parent 43758d4 commit 3d226d9

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

client/events.lua

+15-4
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,34 @@ RegisterNetEvent('QBCore:Command:DeleteVehicle', function()
160160
end)
161161

162162
RegisterNetEvent('QBCore:Client:VehicleInfo', function(info)
163-
local plate = QBCore.Functions.GetPlate(info.vehicle)
164-
local hasKeys = true
163+
-- Check if the info object and vehicle data exist
164+
if not info or not info.vehicle then return end
165+
166+
-- Get the license plate from info.plate or via fallback function
167+
local plate = info.plate or QBCore.Functions.GetPlate(info.vehicle)
168+
if not plate then
169+
print('[ERROR] Failed to get plate from vehicle')
170+
return
171+
end
165172

173+
-- Check if the player has keys (if the resource is running)
174+
local hasKeys = true
166175
if GetResourceState('qb-vehiclekeys') == 'started' then
167-
hasKeys = exports['qb-vehiclekeys']:HasKeys(plate)
176+
hasKeys = exports['qb-vehiclekeys']:HasKeys(plate) -- Optional dependency check
168177
end
169178

179+
-- Collect all necessary vehicle info
170180
local data = {
171181
vehicle = info.vehicle,
172182
seat = info.seat,
173-
name = info.modelName,
183+
name = info.modelName or GetDisplayNameFromVehicleModel(GetEntityModel(info.vehicle)),
174184
plate = plate,
175185
driver = GetPedInVehicleSeat(info.vehicle, -1),
176186
inseat = GetPedInVehicleSeat(info.vehicle, info.seat),
177187
haskeys = hasKeys
178188
}
179189

190+
-- Trigger the event with collected data
180191
TriggerEvent('QBCore:Client:' .. info.event .. 'Vehicle', data)
181192
end)
182193

0 commit comments

Comments
 (0)