Jump to content

Killing the timer


myonlake

Recommended Posts

Hello,

I am doing a fuel script, unfortunatly this repeats the timer2 after the fuel is 0 or less than 0. I want to kill the timer, I've tried killTimer in many ways, it says stack overflow..

Any other way than repeat until (this was infinite ring which caused the server to abort the script). Unless you find a way to do it properly.

So the main problem is that it spams my chatbox with the "Your vehicle ran out of fuel!" and I guess it also spams the engine toggling.

function setFuelOn(thePlayer) 
    local theVehicle = getPedOccupiedVehicle(thePlayer) 
    local get = getElementData(theVehicle, "sfserver.fuel") 
     
    if get == 0 or get < 0 then 
        setVehicleEngineState(theVehicle, false) 
        outputChatBox("This vehicle does not have any fuel!", thePlayer, 255, 0, 0, false) 
    else 
        local timer1 = setTimer( 
        function() 
            setElementData(theVehicle, "sfserver.fuel", get - 1) 
        end, 50, 0) 
     
        local timer2 = setTimer( 
        function() 
            if get == 0 or get < 0 then 
                setVehicleEngineState(theVehicle, false) 
                outputChatBox("Your vehicle ran out of fuel!", thePlayer, 255, 0, 0, false) 
            end 
        end, 1000, 0) 
    end 
end 
addEventHandler("onVehicleEnter", rElement, setFuelOn) 
 

This is just one piece of the code, this is the one which doensn't work.

Thanks

Edited by myonlake
Link to comment

Put important timers in separate functions:

function setFuelOn(thePlayer) 
    local getFuel = getElementData(source, "sfserver.fuel") 
     
    if getFuel <= 0 then 
        setVehicleEngineState(source, false) 
        outputChatBox("This vehicle does not have any fuel!", thePlayer, 255, 0, 0, false) 
    else 
        setTimer ( checkFuel, 1000, 1, thePlayer ) 
    end 
end 
addEventHandler("onVehicleEnter", rElement, setFuelOn) 
  
function checkFuel ( thePlayer ) 
    local theVehicle = getPedOccupiedVehicle(thePlayer) 
     
    if not theVehicle then return end   -- abort 
     
    local getFuel = getElementData(theVehicle, "sfserver.fuel") 
     
    if getFuel <= 0 then 
        setVehicleEngineState(theVehicle, false) 
        outputChatBox("This vehicle does not have any fuel!", thePlayer, 255, 0, 0, false) 
        return  --abort 
    else 
        setElementData(theVehicle, "sfserver.fuel", getFuel - 1) 
    end 
    setTimer ( checkFuel, 1000, 1, thePlayer ) 
end 

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