Jump to content

My fuel script not working


Benzzi

Recommended Posts

Hello my fuel script not working :(

Console: ERROR: bensiini\fuel.lua:115: attempt to compare number with nil

fuel.lua

function takeMoneyFueling (player,money) 
    takePlayerMoney (player,money) 
end 
addEvent ("tkf", true) 
addEventHandler ("tkf", getRootElement(), takeMoneyFueling) 
  
local gas1 = createMarker ( 1007.0478515625, -940.341796875, 41.1759, "cylinder", 3, 255, 255, 0, 150 ) 
local gas2 = createMarker ( 652.5810546875, -570.3798828125, 15.3359, "cylinder", 3, 255, 255, 0, 150 ) 
local gas3 = createMarker ( 2120.912109375, 929.107421875, 9.82, "cylinder", 3, 255, 255, 0, 150 ) 
local gas4 = createMarker ( -1609.2822265625, -2718.388671875, 48.539, "cylinder", 3, 255, 255, 0, 150 ) 
local gas5 = createMarker ( -1673.3056640625, 404.3037109375, 6.1796, "cylinder", 3, 255, 255, 0, 150 ) 
--local gas6 = createMarker ( -1329.119140625, 2671.9931640625, 49.06, "cylinder", 3, 255, 255, 0, 150 ) 
--local gas7 = createMarker ( 1595.681640625, 2197.2509765625, 9.82031, "cylinder", 3, 255, 255, 0, 150 ) 
local gas8 = createMarker ( -1376.40674, -239.16183, 14.14844, "cylinder", 10, 255, 255, 0, 150 ) 
local gas9 = createMarker ( 64, 1220, 19, "cylinder", 3, 255, 255, 0, 150 ) 
local gas10 = createMarker ( 2194, 2480, 11, "cylinder", 3, 255, 255, 0, 150 ) 
local gas11 = createMarker ( 1479.74475, 1813.42822, 10.81250, "cylinder", 10, 255, 255, 0, 150 ) 
local gas12 = createMarker ( 1590, 2196, 11, "cylinder", 3, 255, 255, 0, 150 ) 
local gas13 = createMarker ( 2142, 2740, 11, "cylinder", 3, 255, 255, 0, 150 ) 
local gas14 = createMarker ( -1329, 2671, 50, "cylinder", 3, 255, 255, 0, 150 ) 
local gas15 = createMarker ( -1680, 412, 7, "cylinder", 3, 255, 255, 0, 150 ) 
local gas16 = createMarker ( -2413, 970, 11, "cylinder", 3, 255, 255, 0, 150 ) 
local gas17 = createMarker ( -2244, -2561, 313, "cylinder", 3, 255, 255, 0, 150 ) 
local gas18 = createMarker ( 653, -570, 16, "cylinder", 3, 255, 255, 0, 150 ) 
local gas19 = createMarker ( -94, -1161, 3, "cylinder", 3, 255, 255, 0, 150 ) 
local gas20 = createMarker ( 1945, -1771, 13, "cylinder", 3, 255, 255, 0, 150 ) 
local gas21 = createMarker ( 1008, -939, 42, "cylinder", 3, 255, 255, 0, 150 ) 
local gas22 = createMarker ( -1600, -2706, 49, "cylinder", 3, 255, 255, 0, 150 ) 
local gas23 = createMarker ( -2023, 157, 28, "cylinder", 3, 255, 255, 0, 150 ) 
local gas24 = createMarker ( 1378.5, 458.5, 20, "cylinder", 3, 255, 255, 0, 150 ) 
setElementDimension(gas1, 11000 ) 
setElementDimension(gas2, 11000 ) 
setElementDimension(gas3, 11000 ) 
setElementDimension(gas4, 11000 ) 
setElementDimension(gas5, 11000 ) 
setElementDimension(gas6, 11000 ) 
setElementDimension(gas7, 11000 ) 
setElementDimension(gas8, 11000 ) 
setElementDimension(gas9, 11000 ) 
setElementDimension(gas10, 11000 ) 
setElementDimension(gas11, 11000 ) 
setElementDimension(gas12, 11000 ) 
setElementDimension(gas13, 11000 ) 
setElementDimension(gas14, 11000 ) 
setElementDimension(gas15, 11000 ) 
setElementDimension(gas16, 11000 ) 
setElementDimension(gas17, 11000 ) 
setElementDimension(gas18, 11000 ) 
setElementDimension(gas19, 11000 ) 
setElementDimension(gas20, 11000 ) 
setElementDimension(gas21, 11000 ) 
setElementDimension(gas22, 11000 ) 
setElementDimension(gas23, 11000 ) 
setElementDimension(gas24, 11000 ) 
  
timer = {} 
  
function loadBinds () 
   for key, player in ipairs (getElementsByType("player")) do 
        bindKey(player,"space", "down", gasolina) 
    end 
end 
addEventHandler("onResourceStart", resourceRoot, loadBinds) 
  
function gasolina (player) 
    if getElementType(player) == "player" then 
        if isElementWithinMarker(player,gas1) or isElementWithinMarker(player,gas2) or isElementWithinMarker(player,gas3) or isElementWithinMarker(player,gas4) or isElementWithinMarker(player,gas5) or isElementWithinMarker(player,gas8) or isElementWithinMarker(player,gas9) or isElementWithinMarker(player,gas10) or isElementWithinMarker(player,gas11) or isElementWithinMarker(player,gas12) or isElementWithinMarker(player,gas13) or isElementWithinMarker(player,gas14) or isElementWithinMarker(player,gas15) or isElementWithinMarker(player,gas16) or isElementWithinMarker(player,gas17) or isElementWithinMarker(player,gas17) or isElementWithinMarker(player,gas18) or isElementWithinMarker(player,gas19) or isElementWithinMarker(player,gas20) or isElementWithinMarker(player,gas21) or isElementWithinMarker(player,gas22) or isElementWithinMarker(player,gas23) or isElementWithinMarker(player,gas24) then 
            triggerClientEvent (player,"gasReload",player) 
        end 
    end 
end 
  
function markerEn (hitElement) 
    if source == gas1 or  source == gas2 or  source == gas3 or  source == gas4 or  source == gas5 or  source == gas6 or  source == gas7 or  source == gas8 or  source == gas9 or  source == gas10 or source == gas11 or source == gas12 or source == gas13 or source == gas14 or source == gas15 or source == gas16 or source == gas17 or source == gas18 or source == gas19 or source == gas20 or source == gas21 or source == gas22 or source == gas22 or source == gas23 or source == gas24 then                
        if getElementType(hitElement) == "player" then 
            outputChatBox ("Welcome to refill your vehicle, type /motor to turn off the engine", hitElement, 0, 255, 0) 
        end 
    end 
end 
addEventHandler ("onMarkerHit", getRootElement(), markerEn) 
  
function onPlayerLogggin () 
    timer[source] = setTimer (sendMech,2500,0,source) 
    bindKey(source,"space", "down", gasolina) 
end 
addEventHandler ("onPlayerLogin", getRootElement(), onPlayerLogggin) 
  
function onPlayerQuitt () 
    if isTimer(timer[source]) then killTimer (timer[source]) end 
end 
addEventHandler ("onPlayerQuit", getRootElement(), onPlayerQuitt) 
  
function detect (player) 
    if isPedInVehicle (player) then 
        if getElementData (getPedOccupiedVehicle(player),"fuel") <= 0 then 
            smtm (""..getPlayerName.." needs a mechanic in "..getZoneName(getElementPosition(player)).."", 255, 255, 0) 
        end 
    end 
end 
  
function isVehicleTankEmpty (vehicle) 
    if vehicle then 
        local fuel = getElementData (vehicle,"fuel") 
        if tonumber(fuel) <= 0 then 
            return true 
        end 
        return false 
    end 
end 
  
function refill () 
    for key, vehicles in ipairs (getElementsByType("vehicle")) do 
        local driver = getVehicleOccupant ( vehicles, 0 ) 
        local fuel = getElementData (vehicles,"fuel") 
        if tonumber(fuel) > 0 then 
            local driver = getVehicleOccupant ( vehicles, 0 ) 
            if driver then 
                toggleControl ( driver, "accelerate", true ) 
                toggleControl ( driver, "brake_reverse", true ) 
            end 
        end 
    end 
end 
setTimer (refill,2000,0) 
  
function smtm ( message, color1, color2, color3 ) 
    local mechs = getTeamFromName ("Mechanic") 
    if mechs then 
        local playersCops = getPlayersInTeam ( mechs ) 
        for key, mech in ipairs ( playersCops ) do 
            outputChatBox ( message, mech, color1, color2, color3 ) 
        end 
    end 
end 

Link to comment
  
function takeMoneyFueling (player,money) 
    takePlayerMoney (player,money) 
end 
addEvent ("tkf", true) 
addEventHandler ("tkf", getRootElement(), takeMoneyFueling) 
  
local gas1 = createMarker ( 1007.0478515625, -940.341796875, 41.1759, "cylinder", 3, 255, 255, 0, 150 ) 
local gas2 = createMarker ( 652.5810546875, -570.3798828125, 15.3359, "cylinder", 3, 255, 255, 0, 150 ) 
local gas3 = createMarker ( 2120.912109375, 929.107421875, 9.82, "cylinder", 3, 255, 255, 0, 150 ) 
local gas4 = createMarker ( -1609.2822265625, -2718.388671875, 48.539, "cylinder", 3, 255, 255, 0, 150 ) 
local gas5 = createMarker ( -1673.3056640625, 404.3037109375, 6.1796, "cylinder", 3, 255, 255, 0, 150 ) 
--local gas6 = createMarker ( -1329.119140625, 2671.9931640625, 49.06, "cylinder", 3, 255, 255, 0, 150 ) 
--local gas7 = createMarker ( 1595.681640625, 2197.2509765625, 9.82031, "cylinder", 3, 255, 255, 0, 150 ) 
local gas8 = createMarker ( -1376.40674, -239.16183, 14.14844, "cylinder", 10, 255, 255, 0, 150 ) 
local gas9 = createMarker ( 64, 1220, 19, "cylinder", 3, 255, 255, 0, 150 ) 
local gas10 = createMarker ( 2194, 2480, 11, "cylinder", 3, 255, 255, 0, 150 ) 
local gas11 = createMarker ( 1479.74475, 1813.42822, 10.81250, "cylinder", 10, 255, 255, 0, 150 ) 
local gas12 = createMarker ( 1590, 2196, 11, "cylinder", 3, 255, 255, 0, 150 ) 
local gas13 = createMarker ( 2142, 2740, 11, "cylinder", 3, 255, 255, 0, 150 ) 
local gas14 = createMarker ( -1329, 2671, 50, "cylinder", 3, 255, 255, 0, 150 ) 
local gas15 = createMarker ( -1680, 412, 7, "cylinder", 3, 255, 255, 0, 150 ) 
local gas16 = createMarker ( -2413, 970, 11, "cylinder", 3, 255, 255, 0, 150 ) 
local gas17 = createMarker ( -2244, -2561, 313, "cylinder", 3, 255, 255, 0, 150 ) 
local gas18 = createMarker ( 653, -570, 16, "cylinder", 3, 255, 255, 0, 150 ) 
local gas19 = createMarker ( -94, -1161, 3, "cylinder", 3, 255, 255, 0, 150 ) 
local gas20 = createMarker ( 1945, -1771, 13, "cylinder", 3, 255, 255, 0, 150 ) 
local gas21 = createMarker ( 1008, -939, 42, "cylinder", 3, 255, 255, 0, 150 ) 
local gas22 = createMarker ( -1600, -2706, 49, "cylinder", 3, 255, 255, 0, 150 ) 
local gas23 = createMarker ( -2023, 157, 28, "cylinder", 3, 255, 255, 0, 150 ) 
local gas24 = createMarker ( 1378.5, 458.5, 20, "cylinder", 3, 255, 255, 0, 150 ) 
setElementDimension(gas1, 11000 ) 
setElementDimension(gas2, 11000 ) 
setElementDimension(gas3, 11000 ) 
setElementDimension(gas4, 11000 ) 
setElementDimension(gas5, 11000 ) 
setElementDimension(gas6, 11000 ) 
setElementDimension(gas7, 11000 ) 
setElementDimension(gas8, 11000 ) 
setElementDimension(gas9, 11000 ) 
setElementDimension(gas10, 11000 ) 
setElementDimension(gas11, 11000 ) 
setElementDimension(gas12, 11000 ) 
setElementDimension(gas13, 11000 ) 
setElementDimension(gas14, 11000 ) 
setElementDimension(gas15, 11000 ) 
setElementDimension(gas16, 11000 ) 
setElementDimension(gas17, 11000 ) 
setElementDimension(gas18, 11000 ) 
setElementDimension(gas19, 11000 ) 
setElementDimension(gas20, 11000 ) 
setElementDimension(gas21, 11000 ) 
setElementDimension(gas22, 11000 ) 
setElementDimension(gas23, 11000 ) 
setElementDimension(gas24, 11000 ) 
  
timer = {} 
  
function loadBinds () 
   for key, player in ipairs (getElementsByType("player")) do 
        bindKey(player,"space", "down", gasolina) 
    end 
end 
addEventHandler("onResourceStart", resourceRoot, loadBinds) 
  
function gasolina (player) 
    if getElementType(player) == "player" then 
        if isElementWithinMarker(player,gas1) or isElementWithinMarker(player,gas2) or isElementWithinMarker(player,gas3) or isElementWithinMarker(player,gas4) or isElementWithinMarker(player,gas5) or isElementWithinMarker(player,gas8) or isElementWithinMarker(player,gas9) or isElementWithinMarker(player,gas10) or isElementWithinMarker(player,gas11) or isElementWithinMarker(player,gas12) or isElementWithinMarker(player,gas13) or isElementWithinMarker(player,gas14) or isElementWithinMarker(player,gas15) or isElementWithinMarker(player,gas16) or isElementWithinMarker(player,gas17) or isElementWithinMarker(player,gas17) or isElementWithinMarker(player,gas18) or isElementWithinMarker(player,gas19) or isElementWithinMarker(player,gas20) or isElementWithinMarker(player,gas21) or isElementWithinMarker(player,gas22) or isElementWithinMarker(player,gas23) or isElementWithinMarker(player,gas24) then 
            triggerClientEvent (player,"gasReload",player) 
        end 
    end 
end 
  
function markerEn (hitElement) 
    if source == gas1 or  source == gas2 or  source == gas3 or  source == gas4 or  source == gas5 or  source == gas6 or  source == gas7 or  source == gas8 or  source == gas9 or  source == gas10 or source == gas11 or source == gas12 or source == gas13 or source == gas14 or source == gas15 or source == gas16 or source == gas17 or source == gas18 or source == gas19 or source == gas20 or source == gas21 or source == gas22 or source == gas22 or source == gas23 or source == gas24 then               
        if getElementType(hitElement) == "player" then 
            outputChatBox ("Welcome to refill your vehicle, type /motor to turn off the engine", hitElement, 0, 255, 0) 
        end 
    end 
end 
addEventHandler ("onMarkerHit", getRootElement(), markerEn) 
  
function onPlayerLogggin () 
    timer[source] = setTimer (sendMech,2500,0,source) 
    bindKey(source,"space", "down", gasolina) 
end 
addEventHandler ("onPlayerLogin", getRootElement(), onPlayerLogggin) 
  
function onPlayerQuitt () 
    if isTimer(timer[source]) then killTimer (timer[source]) end 
end 
addEventHandler ("onPlayerQuit", getRootElement(), onPlayerQuitt) 
  
function detect (player) 
    if isPedInVehicle (player) then 
        if getElementData (getPedOccupiedVehicle(player),"fuel") <= 0 then 
            smtm (""..getPlayerName.." needs a mechanic in "..getZoneName(getElementPosition(player)).."", 255, 255, 0) 
        end 
    end 
end 
  
function isVehicleTankEmpty (vehicle) 
    if vehicle then 
        local fuel = getElementData (vehicle,"fuel") 
        if tonumber(fuel) <= 0 then 
            return true 
        end 
        return false 
    end 
end 
  
function refill () 
    for key, vehicles in ipairs (getElementsByType("vehicle")) do 
        local driver = getVehicleOccupant ( vehicles, 0 ) 
        local fuel = getElementData (vehicles,"fuel") or 0 
        if tonumber(fuel) > 0 then 
            local driver = getVehicleOccupant ( vehicles, 0 ) 
            if driver then 
                toggleControl ( driver, "accelerate", true ) 
                toggleControl ( driver, "brake_reverse", true ) 
            end 
        end 
    end 
end 
setTimer (refill,2000,0) 
  
function smtm ( message, color1, color2, color3 ) 
    local mechs = getTeamFromName ("Mechanic") 
    if mechs then 
        local playersCops = getPlayersInTeam ( mechs ) 
        for key, mech in ipairs ( playersCops ) do 
            outputChatBox ( message, mech, color1, color2, color3 ) 
        end 
    end 
end 
  

Link to comment
  • Moderators

Here is a better version of your code. (I didn't touch the functions/commands which i didn't understand the purpose.

local GAS_R, GAS_G, GAS_B, GAS_A = 255, 255, 0, 150 
local gasStations = { 
    --  { x, y, z, size } 
    { 1007.04, -940.34, 41.17, 3 }, 
    { 652.58, -570.38, 15.33, 3 }, 
    { 2120.91, 929.10, 9.82, 3 }, 
    { -1609.28, -2718.38, 48.53, 3 }, 
    { -1673.30, 404.30, 6.17, 3 }, 
    --{ -1329.11, 2671.99, 49.06, 3 }, 
    -- { 1595.68, 2197.25, 9.82, 3 }, 
    { -1376.40, -239.16, 14.14, 10 }, 
    { 64, 1220, 19, 3 }, 
    { 2194, 2480, 11, 3 }, 
    { 1479.74, 1813.42, 10.81, 10 }, 
    { 1590, 2196, 11, 3 }, 
    { 2142, 2740, 11, 3 }, 
    { -1329, 2671, 50, 3 }, 
    { -1680, 412, 7, 3 }, 
    { -2413, 970, 11, 3 }, 
    { -2244, -2561, 313, 3 }, 
    { 653, -570, 16, 3 }, 
    { -94, -1161, 3, 3 }, 
    { 1945, -1771, 13, 3 }, 
    { 1008, -939, 42, 3 }, 
    { -1600, -2706, 49, 3 }, 
    { -2023, 157, 28, 3 }, 
    { 1378.5, 458.5, 20, 3 } 
} 
  
for k, i in ipairs( gasStations ) do 
    createMarker(i[1], i[2], i[3], "cylinder", i[4], GAS_R, GAS_G, GAS_B, GAS_A) 
    setElementDimension(i, 11000) 
end 
  
function takeMoneyFueling (player, money) 
    takePlayerMoney (player, money) 
end 
addEvent ("tkf", true) 
addEventHandler ("tkf", root, takeMoneyFueling) 
  
function loadBinds () 
    for key, player in ipairs (getElementsByType("player")) do 
        bindKey(player, "space", "down", gasolina) 
    end 
end 
addEventHandler("onResourceStart", resourceRoot, loadBinds) 
  
function gasolina (player) 
    if getElementType(player) == "player" then 
        if isPlayerInGasStation(player) then 
            triggerClientEvent (player, "gasReload", player) 
        end 
    end 
end 
  
function isPlayerInGasStation(player) 
    for k, i in ipairs (gasStations) do 
        if isElementWithinMarker(player, i) then 
            return true 
        end 
    end 
    return false 
end 
  
function markerEn (hitElement) 
    if isPlayerInGasStation( hitElement ) and getElementType( hitElement ) == "player" and isPedInVehicle( hitElement ) then 
        outputChatBox ("Welcome to refill your vehicle, type /motor to turn off the engine", hitElement, 0, 255, 0) 
    end 
end 
addEventHandler ("onMarkerHit", root, markerEn) 
  
local timer = {} 
function onPlayerLogggin () 
    timer[source] = setTimer(sendMech, 2500, 0, source) 
    bindKey(source, "space", "down", gasolina) 
end 
addEventHandler ("onPlayerLogin", root, onPlayerLogggin) 
  
function onPlayerQuitt () 
    if isTimer(timer[source]) then killTimer (timer[source]) end 
end 
addEventHandler ("onPlayerQuit", root, onPlayerQuitt) 
  
function detect (player) 
    if isPedInVehicle (player) then 
        if getElementData(getPedOccupiedVehicle(player), "fuel") or 0 <= 0 then 
            smtm (getPlayerName( player ).." needs a mechanic in "..getZoneName(getElementPosition(player)).."", 255, 255, 0) 
        end 
    end 
end 
  
function isVehicleTankEmpty (vehicle) 
    if vehicle then 
        local fuel = getElementData (vehicle, "fuel") or 0 
        if tonumber(fuel) <= 0 then 
            return true 
        end 
        return false 
    end 
end 
  
function refill () 
    for key, vehicle in ipairs (getElementsByType("vehicle")) do 
        local driver = getVehicleOccupant( vehicle, 0 ) 
        local fuel = getElementData (vehicle, "fuel") or 0 
        if tonumber(fuel) > 0 then 
            local driver = getVehicleOccupant ( vehicle, 0 ) 
            if driver then 
                toggleControl ( driver, "accelerate", true ) 
                toggleControl ( driver, "brake_reverse", true ) 
            end 
        end 
    end 
end 
setTimer(refill, 2000, 0) 
  
function smtm ( message, color1, color2, color3 ) 
    local mechs = getTeamFromName ("Mechanic") 
    if mechs then 
        local playersMech = getPlayersInTeam ( mechs ) 
        for key, mech in ipairs ( playersMech ) do 
            outputChatBox ( message, mech, color1, color2, color3 ) 
        end 
    end 
end 
  

- Why did you put markers in dimension 11000 ? are everything in dimension 11000 or did you just want them to be invisible ? in this case, you should put the alpha of the marker to 0 instead.

- What /tkf was supposed to do ? Because what I see is a command that everyone can use to get free money ! i.e: /tkf -5000 will gives me $5000.

- Learn about table and use them when you can (as I just did for the gas stations). Have a look at my isPlayerInGasStation function and see how it reduced the amount of code in your functions like gasolina. I can add a new gas station easily as I just need to add a row to gasStations tabl, no need to modify the rest of the code.

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