Jump to content

Script issue


Captain Cody

Recommended Posts

This should refuel vehicle when the engine is off and the driver hits space, no debugerrors but I know I did it wrong because it aint working

here's the code

function vehicleRefuel(v,m) 
    if not getElementData(m,'gasStation') then return end 
    if getElementType(v) ~= 'vehicle' then return end 
    if getVehicleType(v) ~= 'Automobile' and getVehicleType(v) ~= 'Bike' and getVehicleType(v) ~= 'Monster Truck' and getVehicleType(v) ~= 'Quad' and getVehicleType(v) ~= 'Plane' and getVehicleType(v) ~= 'Boat' and getVehicleType(v) ~= 'Helicopter' then return end 
    local driver = getVehicleOccupants(v); 
    if not driver[0] then return end 
    if getVehicleEngineState(v) then outputChatBox("Please turn your engine off with j, or /engine then hitspace to refuel.",driver[0]); return end 
    addEventHandler( "onClientKey", root, function(button,press)  
    if (press) and button == "space" 
    then 
    if not isElementWithinMarker(v,m) then return end 
    local maxFuel = carFuel[0]; 
    if getPlayerMoney(driver[0]) < 25 then outputChatBox("You can't afford any more fuel.",p); return end 
    if carFuel[getElementModel(v)] then 
        maxFuel = carFuel[getElementModel(v)]; 
    end 
    if getCarFuel(v) >= maxFuel then return end 
    addCarFuel(v,15); 
    takePlayerMoney(driver[0],25); 
    if getCarFuel(v) >= maxFuel then outputChatBox("Your vehicle has been fully refueled.",getVehicleController(v), 255, 255, 255); takeCarFuel(v,getCarFuel(v)-maxFuel); return end 
    setTimer(vehicleRefuel,500,1,v,m); 
        return true 
    end 
    return false 
end ) 
end 
  
  
addEventHandler('onVehicleRefuel',getRootElement(),vehicleRefuel); 
  
function hitTheMarker(e) 
    setTimer(vehicleRefuel,1500,1,e,source); 
end 
addEventHandler('onMarkerHit',getRootElement(),hitTheMarker); 

Any ideas?

Link to comment
  • Moderators

Where are you debug lines mate?

Add one between line 1 and 2.

Add one between line 4 and 5.

Add one between line 6 and 7.

Add one between line 8 and 9.

Add one between line 11 and 12.

Add one between line 13 and 14.

ETC.

And don't add an addEventHandler with "onClientKey" inside this function, because you can't remove that addEventHandler any more. At the end you will have (30+) addEventHandlers, each time you refuel your vehicle another addEventHandler + function will be created and added.

Link to comment

Ok scratch that, to over kill. How would I make it so when you hit and your engine is on it outputs chat box "Already have" then when you turn off engine it refuels?

--Gas station refuel-- 
function vehicleRefuel(v,m) 
    if not getElementData(m,'gasStation') then return end 
    if getElementType(v) ~= 'vehicle' then return end 
    if getVehicleType(v) ~= 'Automobile' and getVehicleType(v) ~= 'Bike' and getVehicleType(v) ~= 'Monster Truck' and getVehicleType(v) ~= 'Quad' and getVehicleType(v) ~= 'Plane' and getVehicleType(v) ~= 'Boat' and getVehicleType(v) ~= 'Helicopter' then return end 
    local driver = getVehicleOccupants(v);w 
    if getVehicleEngineState ( v ) then outputChatBox("Press J or do /engine to turn engine off.",getVehicleController(v), 255, 255, 255); return end 
    if not driver[0] then return end 
    if not isElementWithinMarker(v,m) then return end --### 
    local maxFuel = carFuel[0]; 
    if getPlayerMoney(driver[0]) < 25 then outputChatBox("You cannot afford any more fuel.",getVehicleController(v), 255, 255, 255); return end 
    if carFuel[getElementModel(v)] then 
        maxFuel = carFuel[getElementModel(v)]; 
    end 
    if getCarFuel(v) >= maxFuel then return end 
    addCarFuel(v,15); 
    takePlayerMoney(driver[0],25); 
    if getCarFuel(v) >= maxFuel then outputChatBox("Your vehicle has been 100% refueled.",getVehicleController(v), 255, 255, 255); takeCarFuel(v,getCarFuel(v)-maxFuel); return end 
    setTimer(vehicleRefuel,500,1,v,m); 
end 
  
addEventHandler('onVehicleRefuel',getRootElement(),vehicleRefuel); 
  
function hitTheMarker(e) 
    setTimer(vehicleRefuel,1500,1,e,source); 
end 
addEventHandler('onMarkerHit',getRootElement(),hitTheMarker); 
--Station refuel-- 

Link to comment

1)Player hits marker

2)If players engine is on, it alerts him to turn it off

3)It checks if engine is off

4)If not it keeps checking --What I originally planned to replace this with is when the player presses space it checks, if not alerts driver to turn engine off, if so it continues. / --Which ever would be easier, first option or this./

5)If it is off, it refuels the vehicle

6)After vehicle is fully refueled it alerts the driver.

Link to comment
  • Moderators

Not tested.

But by putting the conditions and actions inside a schematic another scripter can easier understand what he needs to rewrite. I normally don't write somebody else his code, but for this learning point I am making an exception.

Copy the code to your editor for a better view.

function hitTheMarker(vehicle,matchingDimension,marker) 
    if matchingDimension then 
        -- 1) Player hits marker 
        local marker = marker or source -- if marker from the recheck timer or use the source element from the event. 
        if source or -- < if triggered by event(source)  
            isElement(marker) and isElement(vehicle) then -- or < called by timer 
            if getElementData(marker,'gasStation') and getElementType(vehicle) == 'vehicle' then 
                local vehicleType = getVehicleType(vehicle) 
                if vehicleType == 'Automobile' or  
                    vehicleType == 'Bike' or 
                    vehicleType == 'Monster Truck' or 
                    vehicleType == 'Quad' or  
                    vehicleType == 'Plane' or  
                    vehicleType == 'Boat' or 
                    vehicleType == 'Helicopter' then  
                    local driver = getVehicleController(vehicle) 
                    if driver then 
                        -- 2) If players engine is on, it alerts him to turn it off 
                        if getVehicleEngineState ( vehicle ) then  
                            outputChatBox("Press J or do /engine to turn engine off.",driver, 255, 255, 255) 
                             
                            --[[ 4) If not it keeps checking  
                                --What I originally planned to replace this with is when the player presses space it checks,  
                                if not alerts driver to turn engine off, if so it continues. /  
                                --Which ever would be easier, first option or this./ 
                            ]] 
                            setTimer(hitTheMarker,500,1,vehicle,matchingDimension,marker) -- < recheck 
                        else-- 3) It checks if engine is off 
                            -- 5) If it is off, it refuels the vehicle 
                            setTimer(hitTheMarker,1500,1,vehicle,marker,driver) 
                        end 
                    end 
                end 
            end 
        end 
    end 
end 
addEventHandler('onMarkerHit',root,hitTheMarker) 

function vehicleRefuel(vehicle,marker,driver) 
    if isElement(driver) and isElement(marker) and isElement(vehicle) and -- validation 
        getPedOccupiedVehicle(driver) == vehicle and isElementWithinMarker(vehicle,marker) then -- validation 
         
        local maxFuel = carFuel[0] 
        if getPlayerMoney(driver) < 25 then outputChatBox("You cannot afford any more fuel.",driver, 255, 255, 255); return end 
         
        local vehicleModel = getElementModel(vehicle) 
        if carFuel[vehicleModel] then 
            maxFuel = tonumber(carFuel[vehicleModel]) or 0 
        end 
         
        if getCarFuel(vehicle) < maxFuel then  
            addCarFuel(vehicle,15) 
                 
            takePlayerMoney(driver,25) 
             
            -- 6) After vehicle is fully refueled it alerts the driver. 
            if getCarFuel(vehicle) >= maxFuel then outputChatBox("Your vehicle has been 100% refueled.",driver, 255, 255, 255); takeCarFuel(vehicle,getCarFuel(vehicle)-maxFuel); return end 
                 
            setTimer(vehicleRefuel,500,1,vehicle,marker,driver) 
        end 
    end 
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...