Vehicle management, ownership, spawning, and keys system
The Vehicles component handles owned vehicles, temporary vehicle spawning, vehicle keys, garage management, and vehicle state management. It integrates with the inventory, characters, jobs, and properties systems.
Add a vehicle to a character’s ownership.Parameters:
Name
Type
Required
Description
charSID
number
Yes
Character State ID
vehicleHash
number
Yes
Vehicle model hash
vehicleType
number
Yes
Vehicle type (0=car, 1=boat, 2=aircraft)
infoData
table
Yes
Vehicle information (make, model, class, value)
cb
function
Yes
Callback function (success, vehicleData)
properties
table
No
Vehicle customization properties
defaultStorage
table
No
Storage location {Type, Id}
suppliedVIN
string
No
Custom VIN (auto-generated if not provided)
Returns:
Type
Description
function
Calls callback with success status and vehicle data
Example:
-- Server sidelocal characterId = 123local vehicleHash = GetHashKey('adder')Vehicles.Owned:AddToCharacter( characterId, vehicleHash, 0, -- Car type { make = 'Truffade', model = 'Adder', class = 'Super', value = 1000000 }, function(success, vehicleData) if success then print('Vehicle added:', vehicleData.VIN) print('Plate:', vehicleData.RegisteredPlate) -- Give keys to player Vehicles.Keys:Add(source, vehicleData.VIN) else print('Failed to add vehicle') end end, nil, -- Properties (will use defaults) nil, -- Default storage nil -- Auto-generate VIN)
Use Cases:
-- Add vehicle from dealership purchaseAddEventHandler('dealership:server:PurchaseVehicle', function(modelName, price) local src = source local player = Fetch:Source(src) if not player then return end local char = player:GetData('Character') if not char then return end local stateId = char:GetData('SID') local vehicleHash = GetHashKey(modelName) -- Charge player cash local currentCash = char:GetData('Cash') if currentCash >= price then char:SetData('Cash', currentCash - price) -- Add vehicle to character Vehicles.Owned:AddToCharacter( stateId, vehicleHash, 0, { make = GetMakeName(modelName), model = GetModelName(modelName), class = GetVehicleClass(modelName), value = price }, function(success, vehicleData) if success then -- Give keys Vehicles.Keys:Add(src, vehicleData.VIN) -- Notify player TriggerClientEvent('mythic-notifications:client:Send', src, { message = 'Vehicle purchased! Check your garage.', type = 'success' }) end end ) endend)
Get active (spawned) vehicle data by VIN.Parameters:
Name
Type
Required
Description
VIN
string
Yes
Vehicle Identification Number
Returns:
Type
Description
table/boolean
Vehicle DataStore object or false if not spawned
Example:
-- Server sidelocal VIN = 'ABC123456789'local vehicleData = Vehicles.Owned:GetActive(VIN)if vehicleData then print('Vehicle is spawned') print('Entity ID:', vehicleData:GetData('EntityId')) print('Fuel:', vehicleData:GetData('Fuel')) print('Mileage:', vehicleData:GetData('Mileage')) -- Update vehicle data vehicleData:SetData('Fuel', 50)else print('Vehicle is not currently spawned')end
-- Get all character's vehiclesVehicles.Owned:GetAll(0, 0, characterId, function(vehicles) if vehicles then print(string.format('Found %d vehicles', #vehicles)) for k, v in ipairs(vehicles) do print(v.Make, v.Model, v.RegisteredPlate) print('Storage:', v.Storage.Type, v.Storage.Id) print('Spawned:', v.Spawned and 'Yes' or 'No') end endend, nil, nil, false)-- Get vehicles in specific garageVehicles.Owned:GetAll( 0, -- Cars only 0, -- Character owned characterId, -- Owner function(vehicles) -- Display garage menu end, 1, -- Garage storage 'legion_square' -- Garage ID)-- Get job fleet vehicles accessible to playerlocal player = Fetch:Source(source)if not player then returnendlocal char = player:GetData('Character')if not char then returnend-- Get first job from character's Jobs arraylocal jobs = char:GetData('Jobs')local primaryJob = jobs and jobs[1]if primaryJob then Vehicles.Owned:GetAll( 0, -- Cars nil, -- Any owner type nil, -- Any owner function(vehicles) -- Display fleet menu end, nil, -- Any storage nil, -- Any location true, -- Ignore spawned { -- Fleet access check Id = primaryJob.Id, Workplace = primaryJob.WorkplaceId, Level = primaryJob.GradeLevel } )end
Calls callback with (success, vehicleData, entityId)
Example:
-- Server sidelocal VIN = 'ABC123456789'local spawnCoords = vector3(100.0, 200.0, 30.0)local heading = 90.0Vehicles.Owned:Spawn( source, VIN, spawnCoords, heading, function(success, vehicleData, entityId) if success then print('Vehicle spawned:', entityId) print('Plate:', vehicleData.RegisteredPlate) -- Give keys to player Vehicles.Keys:Add(source, VIN) -- Notify client TriggerClientEvent('mythic-vehicles:client:VehicleSpawned', source, entityId) else TriggerClientEvent('mythic-notifications:client:Send', source, { message = 'Failed to spawn vehicle', type = 'error' }) end end)
Garage System Example:
-- Spawn vehicle from garageAddEventHandler('garage:server:SpawnVehicle', function(VIN, spawnLocation) local src = source local player = Fetch:Source(src) if not player then return end local char = player:GetData('Character') if not char then return end local stateId = char:GetData('SID') -- Verify ownership Vehicles.Owned:GetVIN(VIN, function(vehicleData) if vehicleData and vehicleData.Owner.Type == 0 and vehicleData.Owner.Id == stateId then -- Spawn vehicle Vehicles.Owned:Spawn( src, VIN, spawnLocation.coords, spawnLocation.heading, function(success, vehData, entityId) if success then Vehicles.Keys:Add(src, VIN) end end ) end end)end)
Store a spawned vehicle in a garage/property.Parameters:
Name
Type
Required
Description
VIN
string
Yes
Vehicle Identification Number
storageType
number
Yes
Storage type (0=impound, 1=garage, 2=property)
storageId
string
Yes
Storage location ID
cb
function
Yes
Callback function
Example:
-- Server sideVehicles.Owned:Store( 'ABC123456789', 1, -- Garage storage 'legion_square', function(success) if success then print('Vehicle stored successfully') end end)
Force save vehicle data to database immediately.Parameters:
Name
Type
Required
Description
VIN
string
Yes
Vehicle Identification Number
Example:
-- Server sideVehicles.Owned:ForceSave('ABC123456789')-- Use before server shutdownAddEventHandler('onResourceStop', function(resourceName) if resourceName == GetCurrentResourceName() then for VIN, vehicleData in pairs(ACTIVE_OWNED_VEHICLES) do Vehicles.Owned:ForceSave(VIN) end endend)
-- Server sidelocal hasKeys = Vehicles.Keys:Has(source, 'ABC123456789')if hasKeys then print('Player has keys to this vehicle')end-- Check with group keys (police/ems can access without personal keys if on duty)local hasAccess = Vehicles.Keys:Has(source, 'ABC123456789', 'police')
-- Server sideVehicles.Keys:Add(source, 'ABC123456789')-- Give keys when player buys a vehicleAddEventHandler('dealership:vehiclePurchased', function(source, vehicleData) Vehicles.Keys:Add(source, vehicleData.VIN) TriggerClientEvent('mythic-notifications:client:Send', source, { message = 'You received the vehicle keys', type = 'success' })end)
-- Server sideVehicles.Keys:Remove(source, 'ABC123456789')-- Remove keys when vehicle is soldAddEventHandler('dealership:vehicleSold', function(source, VIN) Vehicles.Keys:Remove(source, VIN)end)
Store character vehicle in property garage.Parameters:
Name
Type
Required
Description
source
number
Yes
Player source
VIN
string
Yes
Vehicle Identification Number
propertyId
string
Yes
Property identifier
cb
function
Yes
Callback function
Example:
-- Server sideVehicles.Owned.Properties:Store( source, 'ABC123456789', 'property_123', function(success) if success then TriggerClientEvent('mythic-notifications:client:Send', source, { message = 'Vehicle stored in property garage', type = 'success' }) end end)
Get vehicles stored in property garage.Parameters:
Name
Type
Required
Description
propertyId
string
Yes
Property identifier
ownerStateId
number
No
Filter by owner character SID
cb
function
Yes
Callback function
Example:
-- Server sideVehicles.Owned.Properties:Get( 'property_123', characterId, function(vehicles) if vehicles then print(string.format('Found %d vehicles in property', #vehicles)) end end)
Get count of vehicles in property garage.Parameters:
Name
Type
Required
Description
propertyId
string
Yes
Property identifier
ignoreVIN
string
No
Exclude specific VIN from count
Returns:
Type
Description
number
Vehicle count
Example:
-- Server side (sync)local count = Vehicles.Owned.Properties:GetCount('property_123')print('Vehicles in property:', count)-- Check capacity before storinglocal maxCapacity = 4local currentCount = Vehicles.Owned.Properties:GetCount('property_123', VIN)if currentCount >= maxCapacity then -- Property garage is fullend
Vehicle State: Spawned vehicles have state stored in Entity(vehicle).state including VIN, Owner, Fuel, Damage, Mileage, and more. Access using Entity(veh).state.PropertyName.
Vehicle Saving: Vehicle data is automatically saved when despawned. Use ForceSave() before server shutdown to prevent data loss.