Jump to content

Fuel system


tosfera

Recommended Posts

Hey,

I'm not here for asking a script or so but I could use some help with thinking. Something my mind isn't thinking logical. :lol:

So, i'm going to start and write a fuel system, but I need a way to safe the data, I was thinking about saving the fuel to the element, like the fuel is being saved as data to the car, but when the car explodes, it will be gone (I can reset it then), but the other thing I'm thinking about, when the player is driving the fuel will go down, and getting the elementdata every little second might bring lag? (I think) so I'm kinda,,, lost right now.

Any help would be appreciated!

Link to comment

Right now I'm setting the fuel and pos's every 100ms, client sided. it doesn't bring any lag to the client nor server. Just having some trouble, I fixed it for the server command (/engine) that it starts and stops the fuel loss, but I'm trying to make it so for my button to, it just doens't work. It keeps starting a new timer in the function... O_O

client:

function btnEngineF() 
    local v = getPedOccupiedVehicle( getLocalPlayer() ) 
    if ( v ) then 
        local fuel = getElementData(v, "fuel") 
        if ( tonumber(fuel) > 0.1 ) then 
            local state = getVehicleEngineState(v) 
            if ( state == false ) then 
                setVehicleEngineState(v, true) 
                guiSetText( lblEngineStatus, "Engine status: Started") 
                outputChatBox(getVehicleEngineState(v)) 
            else 
                setVehicleEngineState(v, false) 
                guiSetText( lblEngineStatus, "Engine status: Stopped") 
                outputChatBox(getVehicleEngineState(v)) 
            end 
            triggerServerEvent("fuelUse", getLocalPlayer(), getLocalPlayer()) 
        else 
            errorMsg("You can't start the engine without fuel.") 
        end 
    else 
        errorMsg("You have to be in a vehicle to do this.") 
    end 
end 

server:

addEvent("fuelUse", true) 
function fuelUse(p) 
    local vehicle = getPedOccupiedVehicle(p) 
    local engine = getVehicleEngineState(vehicle) 
    if vehicle and engine then 
        setTimer(fuelUse,1000,1,p); 
        if getVehicleEngineState(vehicle) == false then return end  
         
        local fuel = getElementData(vehicle, "fuel") 
        if fuel == 0 then setVehicleEngineState(vehicle,false); return end  
  
        local newX,newY,newZ = getElementPosition(vehicle); 
        local oldX = getElementData(vehicle,'oldX') or newX; 
        local oldY = getElementData(vehicle,'oldY') or newY; 
        local oldZ = getElementData(vehicle,'oldZ') or newZ; 
        local vel = (getDistanceBetweenPoints2D(oldX,oldY,newX,newY)/70)+(math.random(100,1000)/50000); 
         
        local oldX = setElementData(vehicle,'oldX',newX); 
        local oldY = setElementData(vehicle,'oldY',newY); 
        local oldZ = setElementData(vehicle,'oldZ',newZ) 
        local fuel = getElementData(vehicle, "fuel") 
        setElementData(vehicle, "fuel", tonumber(fuel) - (vel / 4)) 
        outputChatBox(fuel) 
         
        local remainingFuel = getElementData(vehicle, "fuel"); 
        if ( tonumber(remainingFuel) < 0.001) then 
            setElementData(vehicle, "fuel", "0") 
            outputChatBox('Your car has ran out of fuel.',v); 
            setVehicleEngineState(vehicle,false); 
        end 
    end 
end 
addEventHandler("fuelUse", getRootElement(), fuelUse) 

Link to comment

Got it fixed already. To clear it up abit, I made a vehicle panel for the users. That panel has a button called; engine. This will toggle the users vehicle's engine, but it wasn't working right. It keept spamming new timers for the fuel loss. But right now I removed the total function (client side) of the button, and just triggered the /engine command server sided. So it works now. :3

Link to comment

You know, there are a few people rly active on this forum wich I keep seeing as reply's on people their posts;

SolidSnake, 50p, Sasuke, Manve1, X~|Mr.ALM,karthik_184(not that much, but alot), Shazzimal and us! There is 1 more but I couldn't find his name :(. But still, this forum has somuch 1-day flies... Anyways, good work mate. :)

And to all those people I mentioned above, great work! :)

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