Jump to content

[HELP]Decreasing problem


bradio10

Recommended Posts

Hi,

So I am having trouble with decreasing fuel for my fuel system I am making.

function downgradeFuel() 
player = localPlayer 
local veh = getPedOccupiedVehicle(player) 
    if (veh) then 
        fuel = getElementData(veh, "fuelbalance") 
            setElementData(veh, "fuelbalance", tonumber(fuel)-1) 
            newfuel = getElementData(veh, "fuelbalance") 
            outputChatBox("Fuel: "..newfuel, root, 255, 255, 0) 
    end 
end 
addEventHandler("onClientVehicleEnter", root, downgradeFuel) 
  
setTimer(downgradeFuel, 2000, 0) 
  

Every 2 seconds, it outputs the fuel. But, when it does, it only takes 1 away, but doesnt keep decreasing.

So, the default is 100, it then outputs 99 because it took 1 away from 100, but then it just constantly outputs 99.

I want it to decrease, so 98, 97, 96 etc..

There are no errors in the debug either.

Thanks.

Link to comment

You're hooking onClientVehicleEnter, which will only trigger once as you don't have any Timers (this event is called when the player enters the vehicle, as the name implies).

You need to either use a Timer to trigger your function (separately) to constantly decrease fuel, or a better option would be something like I've done here (please don't copy-paste this code, as it isn't guaranteed to work literally as I've wrote it here, as it is untested):

  
function ExitEngineStop(theVeh, seat, hijacked) 
    if seat == 0 and not hijacked then 
        setVehicleEngineState(theVeh, false) 
        setVehicleLocked(theVeh, false) 
    end 
end 
addEventHandler("onPlayerVehicleExit", getRootElement(), ExitEngineStop) 
  
function EnterEngineStop(theVeh, seat, hijacked) 
    if seat == 0 and not hijacked then 
        setVehicleEngineState(theVeh, false) 
        setVehicleLocked(theVeh, false) 
    end 
  
end 
addEventHandler("onPlayerVehicleEnter", getRootElement(), EnterEngineStop) 
  

Create your own events inside those functions (with addEvent) and then use an addEventHandler so that you only deduct the fuel whilst the vehicle's engine is actually running (using setVehicleEngineState). This would be more player friendly as they are no longer forced to use up fuel if they just sit in their car.

Link to comment
You're hooking onClientVehicleEnter, which will only trigger once as you don't have any Timers (this event is called when the player enters the vehicle, as the name implies).

You need to either use a Timer to trigger your function (separately) to constantly decrease fuel, or a better option would be something like I've done here (please don't copy-paste this code, as it isn't guaranteed to work literally as I've wrote it here, as it is untested):

  
function ExitEngineStop(theVeh, seat, hijacked) 
    if seat == 0 and not hijacked then 
        setVehicleEngineState(theVeh, false) 
        setVehicleLocked(theVeh, false) 
    end 
end 
addEventHandler("onPlayerVehicleExit", getRootElement(), ExitEngineStop) 
  
function EnterEngineStop(theVeh, seat, hijacked) 
    if seat == 0 and not hijacked then 
        setVehicleEngineState(theVeh, false) 
        setVehicleLocked(theVeh, false) 
    end 
  
end 
addEventHandler("onPlayerVehicleEnter", getRootElement(), EnterEngineStop) 
  

Create your own events inside those functions (with addEvent) and then use an addEventHandler so that you only deduct the fuel whilst the vehicle's engine is actually running (using setVehicleEngineState). This would be more player friendly as they are no longer forced to use up fuel if they just sit in their car.

I do have a timer, look at the bottom of my code.

Link to comment
Move the timer inside the function, maybe?

I tried this:

function downgradeFuel() 
player = localPlayer 
local veh = getPedOccupiedVehicle(player) 
        if (veh) then 
                fuel = getElementData(veh, "fuelbalance") 
                        setElementData(veh, "fuelbalance", tonumber(getElementData(veh, "fuelbalance"))-1) 
                        outputChatBox("Fuel: "..tonumber(getElementData(veh, "fuelbalance")), root, 255, 255, 0) 
        end 
end 
  
function onEnter() 
        fueltimer = setTimer(downgradeFuel, 2000, 0) 
end 
addEventHandler("onClientVehicleEnter", root, onEnter) 
  
function onExit() 
        if (isTimer(fueltimer)) then 
                killTimer(fueltimer) 
        end 
end 
addEventHandler("onClientVehicleExit", root, onExit) 

But it still doesn't work the way it should.

Link to comment

You should try this.

local fuelTimer; 
  
function downgradeFuel ( element ) 
    if ( isElement ( element ) ) then 
        if ( getVehicleEngineState ( element ) ) then 
            local remainingFuel = getElementData ( element, "fuelbalance" ); 
            if ( remainingFuel ) then 
                if ( tonumber ( remainingFuel ) > 0 ) then 
                    setElementData ( element, "fuelbalance", tonumber ( remainingFuel ) - 1 ); 
                    outputChatBox ( "Remaining fuel: ".. getElementData ( element, "fuelbalance" ) ); 
                else 
                    outputChatBox ( "The engine turns off due to the lack of fuel" ); 
                end 
            else 
                setElementData ( element, "fuelbalance", 0 ); 
            end 
        end 
    end 
end 
  
addEventHandler ( "onClientVehicleEnter", root, 
    function ( thePlayer, theSeat ) 
        if ( ( thePlayer == getLocalPlayer() ) and ( theSeat == 0 ) ) then 
            fuelTimer = setTimer ( downgradeFuel, 2000, -1, getPedOccupiedVehicle ); 
        end 
    end 
); 
  
addEventHandler ( "onClientVehicleExit", root, 
    funciton ( thePlayer, theSeat ) 
        if ( ( thePlayer == getLocalPlayer() ) and ( theSeat == 0 ) ) then 
            if ( isTimer ( fuelTimer ) ) then 
                killTimer ( fuelTimer ); 
            end 
            fuelTimer = nil; 
        end 
    end 
); 

Please note that this doesn't have a function to stop the timer when your engine turns off, it isn't calculating if you're actually moving, how fast you're moving and so forth. It only starts the timer when you enter a vehicle and when the engine is on. Only works if you are on seat 0 ( driver ). Beside that, the data is set in a client sided script, if you're doing this. You have to sync it every 2 seconds with the server too. I'd rather do it server sided, but that's my point of view!

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