Jump to content

[Solved] Fuel System


justn

Recommended Posts

Hey, i'm working on a fuel system but it doesn't work , can someone fix please ? thanks..

No errors in debugscript

fuelNumber = 100 
  
for i, vehicle in ipairs(getElementsByType("vehicle")) do 
setElementData(vehicle,"fuel",tonumber(fuelNumber)) 
end 
  
  
function decreaseFuel(seat, door) 
setTimer(function() 
if ( seat == 0 ) and ( door == 0 ) then 
local veh = getPedOccupiedVehicle(localPlayer) 
if ( veh ) then 
setElementData(veh,"fuel",tonumber(fuelNumber)-1) 
addEventHandler("onClientRender",getRootElement(),onFuelRender) 
end 
end 
end,1000,0) 
end 
addEventHandler("onClientVehicleEnter",getRootElement(),decreaseFuel) 
  
  
function onFuelRender() 
        if isPedInVehicle ( localPlayer ) then 
       dxDrawText("Vehicle Fuel: "..tonumber(fuelNumber).."%", 312, 718, 561, 742, tocolor(255, 255, 255, 255), 1.50, "default-bold", "left", "top", false, false, true, false, false) 
end 
end 

Edited by Guest
Link to comment

Try this, maybe it'll work (not tested)

Client

  
function onFuelRender() 
local fuel = getElementData(localPlayer, "fuel") or 0 
    if isPedInVehicle ( localPlayer ) and fuel then 
       dxDrawText("Vehicle Fuel: "..tonumber(fuel).."%", 312, 718, 561, 742, tocolor(255, 255, 255, 255), 1.50, "default-bold", "left", "top", false, false, true, false, false) 
    end 
end 
addEventHandler("onClientRender", root, onFuelRender) 

Server:

fuelNumber = 100 
  
function fuel () 
    for i, vehicle in ipairs(getElementsByType("vehicle")) do 
     setElementData(vehicle,"fuel",tonumber(fuelNumber)) 
    end 
end 
addEventHandler("onPlayerJoin", root, fuel) 
  
function decreaseFuel(seat, door) 
 setTimer( 
  function() 
    if ( seat == 0 ) and ( door == 0 ) then 
    local veh = getPedOccupiedVehicle(localPlayer) 
        if ( veh ) and getVehicleController( localPlayer ) then 
        setElementData(veh,"fuel",tonumber(fuelNumber)-1) 
        triggerClientEvent(localPlayer,"onClientRender", root) 
        end 
    end 
  end 
 end,1000,0) 
addEventHandler("onClientVehicleEnter",getRootElement(),decreaseFuel) 

Link to comment

Nikolai, why did you give every vehicle the value '100' when someone joins the server ( onPlayerJoin ). This means that whenever someone joins the server, every vehicle will be refilled on the client side. You can better make it server sided and give the data on the onResourceStart. Don't forget to use the resourceRootElement ( getThisResource() ) to make sure it only does it when that resource starts, which is also a bad thing if they want to save the fuel into a database.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...