Jump to content

Made a fuel script for cars but I don't know what am I missing in the server side.. :/


bencskrisz

Recommended Posts

Client side:

function showPlayerFuel()
    hidePlayerFuel(true);
    
    fuelFrame = guiCreateStaticImage( 0.72, 0.79, 0.05, 0.20, "images/Fuel_Gage.png", true)
    fuelBar1 = guiCreateStaticImage( 0.742, 0.811, 0.021, 0.17, "images/Fuel_Dot.png", true)
    guiBringToFront ( fuelFrame )
end
addEvent("FullTank", true )
addEventHandler("FullTank", resourceRoot, showPlayerFuel)
addEvent("fullRefill", true)
addEventHandler("fullRefill",resourceRoot, showPlayerFuel)
addEvent("fuelGageShow", true)
addEventHandler("fuelGageShow", resourceRoot, showPlayerFuel)

function CurrentFuelIn ( currentFuel )
    local sizex = currentFuel["sizex"]
    local sizey = currentFuel["sizey"]
    local posx = currentFuel["posx"]
    local posy = currentFuel["posy"]
    
    hidePlayerFuel(true);
    
    fuelBar1 = guiCreateStaticImage( tonumber(posx), tonumber(posy), tonumber(sizex), tonumber(sizey), "images/Fuel_Dot.png", true)
    fuelFrame = guiCreateStaticImage( 0.72, 0.79, 0.05, 0.20, "images/Fuel_Gage.png", true)
    if(posy and posy > 0.9) then
        if not warningdot1 then
            warningdot1 = guiCreateStaticImage( 0.7288, 0.912, 0.014, 0.016, "images/warning_dot.png", true)
        end
        if((Posy or 0) >= 0.981) then
            setVehicleEngineState(getPedOccupiedVehicle(localPlayer),false)
        else
            outputChatBox("-- You Are Quite Low On Fuel | Refuel at Area 69", 255, 50, 50, true)
            playSoundFrontEnd(4)
        end
    end
end
addEvent("CurrentFuel", true)
addEventHandler("CurrentFuel", resourceRoot, CurrentFuelIn)


function startExit ( vehicle,seat )
    if seat == 0 and fuelBar1 then
        local x, y = guiGetSize( fuelBar1, true )
        local Posx, Posy = guiGetPosition ( fuelBar1, true )
        local fTbl = {}
        fTbl.sizex = x
        fTbl.sizey = y
        fTbl.posx = Posx
        fTbl.posy = Posy
        triggerServerEvent('onGetFuel',vehicle,fTbl)
        hidePlayerFuel()
        
        if(fuelTimer) then
            killTimer(fuelTimer)
            fuelTimer = false;
        end
    end
end
addEventHandler("onClientPlayerVehicleExit", localPlayer, startExit )

function vehEntered ( veh,seat )
    if seat == 0 and isElement(fuelBar1) then
        local Posx, Posy = guiGetPosition ( fuelBar1, true )
        if(Posy and Posy >= 0.981) then
            setVehicleEngineState(veh,false)
        end
    end
    
    if(isTimer(fuelTimer)) then
        killTimer(fuelTimer);
        fuelTimer = nil;
    end
    
    fuelTimer = setTimer ( decFuel1, 2000, 0)
end
addEventHandler("onClientPlayerVehicleEnter", localPlayer, vehEntered )

function decFuel1()
    local PlayerInCar = getPedOccupiedVehicle( getLocalPlayer() )
    if (PlayerInCar and getVehicleController(PlayerInCar) == localPlayer and not isPedDead(localPlayer) ) then
        if(not fuelBar1) then
            showPlayerFuel();
            return;
        end

        local x, y = guiGetSize( fuelBar1, true )
        local Posx, Posy = guiGetPosition ( fuelBar1, true )
        local NewY,NewPosY
        if not Posy then return end
        if((isElementWithinColShape(localPlayer,safeZone) or isElementWithinSafeArea(localPlayer)) and Posy > 0.812) then
            NewY = y + 0.009
            NewPosY = Posy - 0.009
            if(warningdot1) then
                destroyElement(warningdot1)
                warningdot1 = nil
                playSoundFrontEnd(1)
            end
        elseif(Posy < 0.981) then
            local velo = getActualVelocity( PlayerInCar )
            local val = (velo < 5 and 0) or (velo < 100 and 0.0007) or 0.0010 -- 6 / 9
            NewY = y - val
            NewPosY = Posy + val
            if(Posy and (Posy > 0.885 and Posy < 0.95) and not warningdot1) then
                warningdot1 = guiCreateStaticImage( 0.7288, 0.912, 0.014, 0.016, "images/warning_dot.png", true)
                playSoundFrontEnd(4)
                
                outputChatBox("Your vehicle tank is about to run out.", 255, 100, 100);
            end
        elseif (Posy >= 0.981 and getVehicleEngineState(PlayerInCar)) then
            setVehicleEngineState(PlayerInCar,false)
            callServerFunction('setVehicleEngineState',PlayerInCar,false)
        end    
        
        if(NewY) then
            guiSetSize(fuelBar1, x, NewY, true)
        end
        
        if(NewPosY) then
            guiSetPosition(fuelBar1, Posx, NewPosY, true )
        end
    elseif(fuelTimer) then
        hidePlayerFuel()
    end
end

function hidePlayerFuel(bDontDestroyTimer)
    
    if(fuelFrame) then
        destroyElement(fuelFrame)
        fuelFrame = nil;
    end
    
    if(fuelBar1) then
        destroyElement(fuelBar1)
        fuelBar1 = nil;
    end
    
    if(warningdot1) then
        destroyElement(warningdot1);
        warningdot1 = nil;
    end
    
    if(not bDontDestroyTimer and isTimer(fuelTimer)) then
        killTimer(fuelTimer);
        fuelTimer = nil;
    end
    
end
addEvent("jacked", true )
addEventHandler("jacked", resourceRoot, hidePlayerFuel )

function getActualVelocity( element )
    if(element) then x,y,z = getElementVelocity(element)  end
    return math.sqrt(x^2 + y^2 + z^2) * 161
end

 

 

Server side:

function startExit ( vehicle,seat )
    if seat == 0 and fuelBar1 then
        local x, y = guiGetSize( fuelBar1, true )
        local Posx, Posy = guiGetPosition ( fuelBar1, true )
        local fTbl = {}
        fTbl.sizex = x
        fTbl.sizey = y
        fTbl.posx = Posx
        fTbl.posy = Posy
        triggerServerEvent('onGetFuel',vehicle,fTbl)
        hidePlayerFuel()
        
        if(fuelTimer) then
            killTimer(fuelTimer)
            fuelTimer = false;
        end
    end
end

function vehEntered ( veh,seat )
    if seat == 0 and isElement(fuelBar1) then
        local Posx, Posy = guiGetPosition ( fuelBar1, true )
        if(Posy and Posy >= 0.981) then
            setVehicleEngineState(veh,false)
        end
    end
    
    if(isTimer(fuelTimer)) then
        killTimer(fuelTimer);
        fuelTimer = nil;
    end
    
    fuelTimer = setTimer ( decFuel1, 2000, 0)
end

function decFuel1()
    local PlayerInCar = getPedOccupiedVehicle( getLocalPlayer() )
    if (PlayerInCar and getVehicleController(PlayerInCar) == localPlayer and not isPedDead(localPlayer) ) then
        if(not fuelBar1) then
            showPlayerFuel();
            return;
        end

        local x, y = guiGetSize( fuelBar1, true )
        local Posx, Posy = guiGetPosition ( fuelBar1, true )
        local NewY,NewPosY
        if not Posy then return end
        if((isElementWithinColShape(localPlayer,safeZone) or isElementWithinSafeArea(localPlayer)) and Posy > 0.812) then
            NewY = y + 0.009
            NewPosY = Posy - 0.009
            if(warningdot1) then
                destroyElement(warningdot1)
                warningdot1 = nil
                playSoundFrontEnd(1)
            end
        elseif(Posy < 0.981) then
            local velo = getActualVelocity( PlayerInCar )
            local val = (velo < 5 and 0) or (velo < 100 and 0.0007) or 0.0010 -- 6 / 9
            NewY = y - val
            NewPosY = Posy + val
            if(Posy and (Posy > 0.885 and Posy < 0.95) and not warningdot1) then
                warningdot1 = guiCreateStaticImage( 0.7288, 0.912, 0.014, 0.016, "images/warning_dot.png", true)
                playSoundFrontEnd(4)
                
                outputChatBox("Your vehicle tank is about to run out.", 255, 100, 100);
            end
        elseif (Posy >= 0.981 and getVehicleEngineState(PlayerInCar)) then
            setVehicleEngineState(PlayerInCar,false)
            callServerFunction('setVehicleEngineState',PlayerInCar,false)
        end    
        
        if(NewY) then
            guiSetSize(fuelBar1, x, NewY, true)
        end
        
        if(NewPosY) then
            guiSetPosition(fuelBar1, Posx, NewPosY, true )
        end
    elseif(fuelTimer) then
        hidePlayerFuel()
    end
end

function hidePlayerFuel(bDontDestroyTimer)
    
    if(fuelFrame) then
        destroyElement(fuelFrame)
        fuelFrame = nil;
    end
    
    if(fuelBar1) then
        destroyElement(fuelBar1)
        fuelBar1 = nil;
    end
    
    if(warningdot1) then
        destroyElement(warningdot1);
        warningdot1 = nil;
    end
    
    if(not bDontDestroyTimer and isTimer(fuelTimer)) then
        killTimer(fuelTimer);
        fuelTimer = nil;
    end
    
end
addEvent("jacked", true )
addEventHandler("jacked", resourceRoot, hidePlayerFuel )

function getActualVelocity( element )
    if(element) then x,y,z = getElementVelocity(element)  end
    return math.sqrt(x^2 + y^2 + z^2) * 161
end

 

Edited by CodyJ(L)
Fixed Lua stuff.
Link to comment

Sorry for the way I posted..:|

So when I start the resource, the fuel line appears on the screen when I'm in car but the line does not move so yeah it does not work at all.. when I get out from the vehicle I got an error message -> ERROR: Client triggered serverside event onGetFuel, but event is not added serverside. 

I understand what it says but I'm a bit new in scripting and I don't know what to modify or add on server side cause I added this onGetFuel but obviously not in a proper way

Link to comment

Why your server side is identical to your client side? The error message clearly says that the event "onGetFuel" is not added to server side, and you haven't created anything in server side named onGetFuel therefore it is not working. I believe this is not even your code, am I right?

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