Jump to content

Create Fuel System


Recommended Posts

Hi everyone, I want to make a fuel system. I made this code, but it doesn't working. In debugscript isn't any error and warning. Please help me :/

function stworzPaliwo ( ) 
local v = getPedOccupiedVehicle( getLocalPlayer() ) 
    if ( v ) then 
        local fuel = getElementData( v, "fuel" ) 
            if fuel == nil then 
                setElementData ( v, "fuel", 50 ) 
                outputChatBox ( "Stan paliwa: ".. fuel, 255, 255, 255, false ) 
        end 
    end 
end 
addEventHandler ( "onClientVehicleEnter", getRootElement(), stworzPaliwo ) 

Link to comment
function stworzPaliwo ( ) 
local v = getPedOccupiedVehicle( getLocalPlayer() ) 
    if ( v ) then 
        local fuel = getElementData( v, "fuel" ) 
            if not fuel  then 
                setElementData ( v, "fuel", 50 ) 
                outputChatBox ( "Stan paliwa: ".. fuel, 255, 255, 255, false ) 
        end 
    end 
end 
addEventHandler ( "onClientVehicleEnter", getRootElement(), stworzPaliwo ) 

P.S getElementData return's "false" if there is no data and not "nil"

Link to comment
function stworzPaliwo ( ) 
local v = getPedOccupiedVehicle( getLocalPlayer() ) 
    if ( v ) then 
        local fuel = getElementData( v, "fuel" ) 
            if not fuel  then 
                setElementData ( v, "fuel", 50 ) 
                fuel = 50 -- or  getElementData ( v, "fuel")   it's the same thing 
                outputChatBox ( "Stan paliwa: "..fuel, 255, 255, 255, false ) 
        end 
    end 
end 
addEventHandler ( "onClientVehicleEnter", getRootElement(), stworzPaliwo ) 

Link to comment
function stworzPaliwo ( ) 
local v = getPedOccupiedVehicle( getLocalPlayer() ) 
    if ( v ) then 
        local fuel = getElementData( v, "fuel" ) 
            if not fuel  then 
                setElementData ( v, "fuel", 50 ) 
                fuel = 50  
                outputChatBox ( "Stan paliwa: "..fuel, 255, 255, 255, false ) -- in case the car is a new one and have no fuel 
            else 
                outputChatBox ( "This car have  "..fuel.." left.", 255, 255, 255, false ) -- in case if the car already have fuel (it was used one or more time) 
        end 
    end 
end 
addEventHandler ( "onClientVehicleEnter", getRootElement(), stworzPaliwo ) 

Link to comment

It takes a fuel, but only if I switch engine on/off.. Idk where is mistake it's my first script with using setTimer.

serverside:

function offEngine ( source, thePlayer ) 
local vehicle = getElementData ( source, "vehicle_owner" ) 
local acc = getPlayerAccount ( source ) 
if (not acc or isGuestAccount ( acc )) then return end 
local accName = getAccountName ( acc ) 
    if vehicle then 
    local vehicleName = getVehicleName ( vehicle ) 
        if ( getElementData ( vehicle, "veh_owner" ) == accName ) then 
            if getVehicleEngineState ( vehicle ) ==  true then 
                outputChatBox ( "[iNFO] Engine: Off !", source, 255, 255, 0, true ) 
                setVehicleEngineState ( vehicle, false ) 
            else 
                outputChatBox ( "[iNFO] Engine: On !", source, 255, 255, 0, true ) 
                setVehicleEngineState ( vehicle, true ) 
        end 
    end 
    triggerClientEvent ( "useFuel", source, source ) 
    else 
    vehicle = nil 
end 
end 

clientside:

function usingFuel ( ) 
local vehicle = getPedOccupiedVehicle ( getLocalPlayer() ) 
local engine = getVehicleEngineState ( vehicle ) 
local fuel = getElementData ( vehicle, "fuel" ) 
    if ( vehicle ) and ( engine == true ) and ( tonumber(fuel) >= 1 ) then 
        actualFuel = tonumber(getElementData ( vehicle, "fuel" )) 
        local timer = setTimer ( function ( ) 
                    setElementData ( pojazd, "fuel", tonumber(actualFuel) - 1 ) 
                    outputChatBox ( "Fuel: " .. tonumber(actualFuel), 255, 255, 255, false ) 
                end, 2000, 0 ) 
    elseif ( vehicle ) and ( engine == true ) and ( getElementData ( vehicle, "fuel" ) == 0 ) and isTimer ( timer ) then 
        killTimer ( timer ) 
        setVehicleEngineState ( theVehicle, false ) 
    else 
        killTimer ( timer ) 
    end 
end 
addEvent ( "useFuel", true ) 
addEventHandler ( "useFuel", getRootElement(), usingFuel ) 

Link to comment
  • Moderators

You can try this at client side:

local timer 
  
local fuelFunction = function (vehicle) 
    if isElement(vehicle) then 
        local actualFuel = tonumber(getElementData ( vehicle, "fuel" )) or 0 
        if actualFuel >= 1 then 
            setElementData ( vehicle, "fuel", actualFuel - 1 ) 
            outputChatBox ( "Fuel: " .. actualFuel, 255, 255, 255, false ) 
        elseif actualFuel <= 0  then 
            killTimer ( timer ) 
            setVehicleEngineState ( vehicle, false ) 
        end 
    end 
end 
  
  
addEvent ( "useFuel", true ) 
local usingFuel = function ( ) 
    local vehicle = getPedOccupiedVehicle (localPlayer) 
    local timerCheck = isTimer ( timer ) 
    if vehicle then 
        local engine = getVehicleEngineState ( vehicle ) 
        if engine then 
            local actualFuel = tonumber(getElementData ( vehicle, "fuel" )) or 0 
            if not timerCheck and actualFuel >= 1 then 
                timer = setTimer ( fuelFunction, 2000, 0,vehicle) 
            elseif actualFuel <= 0  then 
                if timerCheck then 
                    killTimer ( timer ) 
                end 
                setVehicleEngineState ( vehicle, false ) 
            end 
        elseif timerCheck then 
            killTimer ( timer ) 
        end 
    elseif timerCheck then 
        killTimer ( timer ) 
    end 
end 
addEventHandler ( "useFuel", root, usingFuel ) 

Edited by Guest
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...