FiveM vehicle fuel: set fuel on spawn and fix cars stuck at 0
How fuel works in FiveM: GetVehicleFuelLevel and SetVehicleFuelLevel, fuel in state bags, setting fuel when you spawn a car, and why vehicles start with an empty tank.
Typical symptoms:
A freshly spawned car has the fuel gauge at 0 and the engine cuts out after a few metres.Fuel in FiveM is not one system. The game has a native for it, but almost every server runs a fuel resource that keeps its own value. This article explains the natives, where fuel resources store the level, and how to set fuel correctly when you spawn a car.
The two natives
The game stores a fuel level on every vehicle. You read and write it on the client:
local vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
local fuel = GetVehicleFuelLevel(vehicle)
print('fuel', fuel)
SetVehicleFuelLevel(vehicle, 100.0)GetVehicleFuelLevel returns a float and SetVehicleFuelLevel takes a float. The value follows the petrol tank volume from the vehicle's handling, so scripts normally treat it as a percentage and keep it between 0 and 100.
On its own, the native does not consume fuel. The game only drains it in specific situations, so a fuel resource is what makes cars use fuel based on speed and RPM.
Where fuel resources keep the level
This is the part that causes most problems. A fuel resource has to remember the level of each car, and it picks one of these places:
- A state bag on the entity. Some resources keep the level in the vehicle's state, for example
Entity(vehicle).state.fuel. This is how ox_fuel works, and several other modern resources do the same. See state bags for how the values sync between server and clients. - A decorator. Older resources store the level with
DecorSetFloaton the vehicle. - The native only. The simplest scripts read
GetVehicleFuelLeveland writeSetVehicleFuelLeveland nothing else. - The database. Many resources also save the level of owned cars so it persists between sessions, in a
fuelcolumn of the vehicle table.
A native call that is not backed by the place your resource reads has no visible effect. If you call SetVehicleFuelLevel(vehicle, 100.0) and the gauge stays at 0, your resource reads another source.
Tip: open the fuel resource's files and search for
state.fuel,DecorandGetVehicleFuelLevel. Whichever it reads is the one you have to write.
Set fuel when you spawn a car
Whatever your fuel resource uses, the order is the same. Create the vehicle, wait for it to exist, then set the fuel:
local vehicle = CreateVehicle(model, coords.x, coords.y, coords.z, heading, true, false)
SetVehicleNumberPlateText(vehicle, plate)
-- 1. the native, for the game's own value
SetVehicleFuelLevel(vehicle, 100.0)
-- 2. the state bag, if your fuel resource reads one
Entity(vehicle).state:set('fuel', 100.0, true)Use only the line that your resource reads. Writing a state bag key that nothing reads is harmless but useless.
Some resources also provide an export to set the level so you do not need to know where it is stored. If yours does, prefer it, and take the exact name from its documentation:
-- placeholder: the export name depends on your fuel resource
exports['your_fuel_resource']:SetFuel(vehicle, 100.0)For a car built on the server with CreateVehicleServerSetter, set the value server side through the state bag. The native SetVehicleFuelLevel runs on the client, so for server created cars you hand the net id to the owner and set it there, as described in spawning vehicles.
Owned cars come out empty
If cars from your garage start with the same low value every time, the garage restores a saved value that is empty. Check the vehicle table, which usually holds the last known level in a column:
SELECT plate, fuel FROM player_vehicles WHERE plate = 'MIC 001';Common reasons:
- The
fuelcolumn is0orNULLbecause the car was stored while the value was not saved. - The garage script never reads the column when spawning.
- The fuel resource saves its own value somewhere else than the garage reads it.
To repair vehicles that are already stuck, set the column to a full tank:
UPDATE player_vehicles SET fuel = 100 WHERE fuel IS NULL OR fuel <= 0;ESX servers often have no fuel column in owned_vehicles, since the fuel is saved inside the vehicle JSON column along with the other properties. See vehicle properties for how that JSON is built.
Cars stuck at 0 fuel
Work through the usual causes:
The fuel resource is not running or started late. If it starts after the garage, the first cars spawned have no value. Start it before the scripts that spawn vehicles.
Two resources fight. A fuel resource and a script that calls SetVehicleFuelLevel every frame overwrite each other, and the gauge jumps. Keep one source.
The level is set before the entity exists. On a networked spawn the call has to happen after DoesEntityExist is true, or it applies to nothing.
The model has a tiny tank. The petrol tank volume comes from the handling. A custom vehicle with fPetrolTankVolume at a very low value empties fast. Check the vehicle's handling file, see editing vehicle handling.
Electric vehicles. A fuel resource that does not exclude them drains a tank that does not exist. Most resources have a list of electric models to skip.
Refuel without a station
For a test or a job vehicle, a small loop keeps the tank full while the player drives it:
CreateThread(function()
while true do
local vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
if vehicle ~= 0 and GetVehicleFuelLevel(vehicle) < 20.0 then
SetVehicleFuelLevel(vehicle, 100.0)
end
Wait(2000)
end
end)The two second Wait is deliberate: fuel does not need a check every frame, see how Wait works.
Checklist
| Symptom | Fix |
|---|---|
| Spawned car has 0 fuel | Set the fuel after the spawn, where your fuel resource reads it |
SetVehicleFuelLevel changes nothing |
The resource reads a state bag or decorator: write that instead, or use its export |
| Garage cars come out empty | Check the fuel column or the saved properties, and make sure the garage restores them |
| Fuel jumps up and down | Two scripts set the level: keep one |
| Server created car has no fuel | Set the state bag on the server, or send the net id to the owner |
| Empty tank on a custom car | Check fPetrolTankVolume in its handling |
| Electric car loses fuel | Add the model to the fuel resource's electric list |
Quick answers
Why does a car I spawn have no fuel?
The fuel resource usually stores the level in its own place, such as a state bag, and a new vehicle has no value there yet. Set the fuel through your fuel resource right after the spawn.
Is SetVehicleFuelLevel enough to fill a tank?
Only for the game's own value. If your fuel resource reads fuel from a state bag or its own table, it ignores the native, so set the value where the resource reads it.
Can I make fuel consumption unlimited?
Yes. Keep the level at the maximum on a timer, or turn consumption off in your fuel resource's config if it has that option.
Scripts that skip this problem
Advanced BoostingTablet-driven vehicle boosting: contracts from class D to S+, crews and a live queue.View script →
Chameleon Paints82 colour-shifting chameleon paints in one click-to-apply menu.View script →
Car BombPlant, detect and defuse vehicle bombs — with a detonator phone and a C4 minigame.View script →