Jump to content

Help Making Fuel system


xXMADEXx

Recommended Posts

Im trying to make my fuel system, but i get an error. I dont know how to fix it, here is my code to set the vehicle fuel:

(server)

setTimer( 
    function () 
        for i,v in ipairs (getElementsByType("vehicle")) do 
            if (getElementData(v,"fuel")=="") then -- I have also tried: if (getElementData(v,"fuel")==nil) then 
                setElementData(v,"fuel",'50',true) 
                outputChatBox(v) 
            end 
        end 
    end, 500, 0 
) 

And, here is my code to get the fuel.

local fuel = getElementData(getPedOccupiedVehicle(localPlayer),"fuel") 
dxDrawBorderedText("Fuel: "..fuel.."%", screenWidth*0.750, screenHeight*0.9, screenWidth, screenHeight, tocolor(255, 255, 255), scale, "arial", "left", "center") 

and the error:

attempt to concatenate local "fuel" (a boolean value) 

Link to comment

Why are you storing fuel as a string? store it as an integer, and then use the 'tostring' to output it in your directX drawing.

setTimer( 
    function( ) 
        for i, v in ipairs ( getElementsByType("vehicle") ) do 
            if ( tonumber( getElementData(v, "fuel") ) == 0 ) then 
                setElementData( v, "fuel", 50, true) 
                -- outputChatBox( v ) 'v' is a vehicle element, you can't output it to chatbox.. 
            end 
        end 
    end, 500, 0 
) 

 local fuel = getElementData( getPedOccupiedVehicle(localPlayer), "fuel" ) 
dxDrawBorderedText("Fuel: ".. tostring( fuel ) .."%", screenWidth*0.750, screenHeight*0.9, screenWidth, screenHeight, tocolor(255, 255, 255), scale, "arial", "left", "center") 

Link to comment

Thanks for that, but there is a new issue. For some reason, when the engine dies, it just turns back on :(

setTimer( 
    function () 
        for i,v in ipairs (getElementsByType("vehicle")) do 
            if (tonumber(getElementData(v,"fuel"))==1) and (getVehicleEngineState(v)==true) then 
                setVehicleEngineState(v,false) 
            elseif (tonumber(getElementData(v,"fuel")) > 0) and (getVehicleEngineState(v)==false) then 
                setVehicleEngineState(v,true) 
            end 
        end 
    end, 1000, 0 
) 

Link to comment
setTimer( 
    function () 
        for i,v in ipairs (getElementsByType("vehicle")) do 
            if (tonumber(getElementData(v,"fuel")) <=1) and (getVehicleEngineState(v)==true) then 
                setVehicleEngineState(v,false) 
            elseif (tonumber(getElementData(v,"fuel")) >= 0) and (getVehicleEngineState(v)==false) then 
                setVehicleEngineState(v,true) 
            end 
        end 
    end, 1000, 0 
) 

try this :)

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...