VehicleHandle
A VehicleHandle refers to a networked vehicle: cars, bikes, boats, planes, helicopters and drones. You get one from World.SpawnVehicle, from vehicle events (OnPlayerVehicleEnter, OnVehicleDamage, OnVehicleDestroy, ...; see Server events and Client events), from player:GetVehicle(), or by casting an ObjectHandle with AsVehicle().
Use it to read and change the vehicle's paint, doors and lock state on the server, to command drones, and on the client to read the vehicle's driving state.
Available on: Server, Client
See also: VehicleHandle (server), VehicleHandle (client)
A VehicleHandle is also an ObjectHandle: position, rotation, velocity, health, script data and the casts work on it too (see that page). If the vehicle no longer exists, or on a client is not streamed in (see Streaming), every setter does nothing and every getter returns nil.
The vehicle's paint, part states, lock state and drone orders are kept on the server and sent to every player who sees the vehicle, including players who come into range later.
Methods
vehicle:GetId()
Returns the model name the vehicle was spawned with.
Side: Server, Client
Parameters: none
Returns: string, for example "v017_car_vintagemuscle_civilian" (see Vehicles), or nil if the vehicle is gone.
Example
Event.Add("OnPlayerVehicleEnter", function(player, vehicle, seat)
print("entered a " .. vehicle:GetId())
end)
vehicle:GetPrimaryColor()
vehicle:GetSecondaryColor()
vehicle:GetTertiaryColor()
Return the paint tints last set with the matching setter. On a client they return the synced value.
Side: Server, Client
Parameters: none
Returns: vec3 with the color in x (red), y (green), z (blue), or nil if the vehicle is gone.
Example
-- copy the paint of one vehicle onto another
local c1, c2, c3 = source:GetPrimaryColor(), source:GetSecondaryColor(), source:GetTertiaryColor()
target:SetPrimaryColor(c1.x, c1.y, c1.z)
target:SetSecondaryColor(c2.x, c2.y, c2.z)
target:SetTertiaryColor(c3.x, c3.y, c3.z)
vehicle:GetSpecularGloss()
vehicle:GetClearCoat()
vehicle:GetMetallic()
Return the paint values last set with the matching setter (or the defaults: gloss 1, clear coat 0.5, metallic 1).
Side: Server, Client
Parameters: none
Returns: number, or nil if the vehicle is gone.
Example
print(string.format("gloss %.2f, clear coat %.2f, metallic %.2f", vehicle:GetSpecularGloss(), vehicle:GetClearCoat(), vehicle:GetMetallic()))
vehicle:GetDirt()
Returns the dirt amount last set with SetDirt.
Side: Server, Client
Parameters: none
Returns: number, or nil if the vehicle is gone.
Example
if vehicle:GetDirt() > 0.5 then
vehicle:SetDirt(0)
end
vehicle:GetPartState(part)
Returns the last known state of one of the vehicle's parts, whether a script or a player changed it. All parts start Closed.
Side: Server, Client
Parameters
| Name | Type | Description |
|---|---|---|
| part | VehiclePart | the part to read |
Returns: VehiclePartState value (integer), or nil if the vehicle is gone.
Example
if vehicle:GetPartState(VehiclePart.Trunk) == VehiclePartState.Open then
print("the trunk is open")
end