Both.
function setFuelCar(thePlayer)
local source = getPedOccupiedVehicle(thePlayer) -- (1)
if getPedOccupiedVehicleSeat(thePlayer)==0 then -- (2)
if getVehicleEngineState(source)==true then ---------if the engine it's on I start the timer -- (3)
local x,y,z = getElementPosition(source)
local fx,fy,fz = x+0.05, y+0.05,z+0.05 -- (4)
local fuelConsumption = 0.900
local fuelVeh = getElementData(source, "Fuel") or 0
if (fuelVeh <= 0) then
setVehicleEngineState(source, false)
else
timerF = setTimer( function () -- (5)
local enginePlus = 0.200
local distance = getDistanceBetweenPoints3D(x,y,z,fx,fy,fz) -- (6)
local newFuel = tonumber((fuelVeh - (fuelConsumption*(distance+enginePlus))))
setElementData(source, "Fuel", newFuel)
end, 20000, 0)
end
end
end
end
addEventHandler("onVehicleEnter", root, setFuelCar)
You don't need to overwrite the source variable. It already exists thanks to the onVehicleEnter event. Have a look at: https://forum.multitheftauto.com/topic/33407-list-of-predefined-variables/#comment-337231
onVehicleEnter already gives you the seat number, you have to "grab" it too :
function setFuelCar(thePlayer, seat)
if seat == 0 then
Here in your code, this check is only done once, when entering the vehicle. You have to change it so that you create the fuel timer anyway when entering the vehicle at seat 0 but inside the timer function, the 1st thing you do is to check the engine state. If it's ON, do the newFuel calculation thing. And if fuel is <= 0 then set it to OFF.
(Why the +0.05 to each components ? seems totaly useless)
You can't use a global variable on the server side or else if another player enters another vehicle, it will start his vehicle fuel timer and override the value in timerF so you have no way to kill the timer of your vehicle later. Must be local and "link it" with your vehicle element: setElementData(source, "fuelTimer", timerF, false) (false because there is no need to sync this value with all connected clients, only the server needs to know and manipulate it).
You have to update the value of fx,fy,fz with the current x, y, z at the end to calculate the new distance traveled between point B and point C (20 secs later). Again, use element data to store that position for the next fuel consumption calculations (you can't use global variables).