Mefisto_PL Posted July 31, 2013 Posted July 31, 2013 Hi everyone, I want to make a fuel system. I made this code, but it doesn't working. In debugscript isn't any error and warning. Please help me function stworzPaliwo ( ) local v = getPedOccupiedVehicle( getLocalPlayer() ) if ( v ) then local fuel = getElementData( v, "fuel" ) if fuel == nil then setElementData ( v, "fuel", 50 ) outputChatBox ( "Stan paliwa: ".. fuel, 255, 255, 255, false ) end end end addEventHandler ( "onClientVehicleEnter", getRootElement(), stworzPaliwo )
bandi94 Posted July 31, 2013 Posted July 31, 2013 function stworzPaliwo ( ) local v = getPedOccupiedVehicle( getLocalPlayer() ) if ( v ) then local fuel = getElementData( v, "fuel" ) if not fuel then setElementData ( v, "fuel", 50 ) outputChatBox ( "Stan paliwa: ".. fuel, 255, 255, 255, false ) end end end addEventHandler ( "onClientVehicleEnter", getRootElement(), stworzPaliwo ) P.S getElementData return's "false" if there is no data and not "nil"
Mefisto_PL Posted July 31, 2013 Author Posted July 31, 2013 Hmm.. still any error and don't working. ///EDIT If i give a car from admin panel I see it in debug: "attempt co concatenate local 'fuel' ( a boolean value )" at line where is outputChatBox
bandi94 Posted August 1, 2013 Posted August 1, 2013 function stworzPaliwo ( ) local v = getPedOccupiedVehicle( getLocalPlayer() ) if ( v ) then local fuel = getElementData( v, "fuel" ) if not fuel then setElementData ( v, "fuel", 50 ) fuel = 50 -- or getElementData ( v, "fuel") it's the same thing outputChatBox ( "Stan paliwa: "..fuel, 255, 255, 255, false ) end end end addEventHandler ( "onClientVehicleEnter", getRootElement(), stworzPaliwo )
Mefisto_PL Posted August 1, 2013 Author Posted August 1, 2013 Okay, it's working, but only if I give a car with admin panel. When I leave and enter then text isn't created.
bandi94 Posted August 1, 2013 Posted August 1, 2013 function stworzPaliwo ( ) local v = getPedOccupiedVehicle( getLocalPlayer() ) if ( v ) then local fuel = getElementData( v, "fuel" ) if not fuel then setElementData ( v, "fuel", 50 ) fuel = 50 outputChatBox ( "Stan paliwa: "..fuel, 255, 255, 255, false ) -- in case the car is a new one and have no fuel else outputChatBox ( "This car have "..fuel.." left.", 255, 255, 255, false ) -- in case if the car already have fuel (it was used one or more time) end end end addEventHandler ( "onClientVehicleEnter", getRootElement(), stworzPaliwo )
Mefisto_PL Posted August 2, 2013 Author Posted August 2, 2013 It takes a fuel, but only if I switch engine on/off.. Idk where is mistake it's my first script with using setTimer. serverside: function offEngine ( source, thePlayer ) local vehicle = getElementData ( source, "vehicle_owner" ) local acc = getPlayerAccount ( source ) if (not acc or isGuestAccount ( acc )) then return end local accName = getAccountName ( acc ) if vehicle then local vehicleName = getVehicleName ( vehicle ) if ( getElementData ( vehicle, "veh_owner" ) == accName ) then if getVehicleEngineState ( vehicle ) == true then outputChatBox ( "[iNFO] Engine: Off !", source, 255, 255, 0, true ) setVehicleEngineState ( vehicle, false ) else outputChatBox ( "[iNFO] Engine: On !", source, 255, 255, 0, true ) setVehicleEngineState ( vehicle, true ) end end triggerClientEvent ( "useFuel", source, source ) else vehicle = nil end end clientside: function usingFuel ( ) local vehicle = getPedOccupiedVehicle ( getLocalPlayer() ) local engine = getVehicleEngineState ( vehicle ) local fuel = getElementData ( vehicle, "fuel" ) if ( vehicle ) and ( engine == true ) and ( tonumber(fuel) >= 1 ) then actualFuel = tonumber(getElementData ( vehicle, "fuel" )) local timer = setTimer ( function ( ) setElementData ( pojazd, "fuel", tonumber(actualFuel) - 1 ) outputChatBox ( "Fuel: " .. tonumber(actualFuel), 255, 255, 255, false ) end, 2000, 0 ) elseif ( vehicle ) and ( engine == true ) and ( getElementData ( vehicle, "fuel" ) == 0 ) and isTimer ( timer ) then killTimer ( timer ) setVehicleEngineState ( theVehicle, false ) else killTimer ( timer ) end end addEvent ( "useFuel", true ) addEventHandler ( "useFuel", getRootElement(), usingFuel )
Sasu Posted August 2, 2013 Posted August 2, 2013 In the client side, At line 8, in the first argument of setElementData, Where did you define "pojazd"?
Mefisto_PL Posted August 2, 2013 Author Posted August 2, 2013 I translate it only. Pojazd mean vehicle, mistake in translating.
Moderators IIYAMA Posted August 2, 2013 Moderators Posted August 2, 2013 (edited) You can try this at client side: local timer local fuelFunction = function (vehicle) if isElement(vehicle) then local actualFuel = tonumber(getElementData ( vehicle, "fuel" )) or 0 if actualFuel >= 1 then setElementData ( vehicle, "fuel", actualFuel - 1 ) outputChatBox ( "Fuel: " .. actualFuel, 255, 255, 255, false ) elseif actualFuel <= 0 then killTimer ( timer ) setVehicleEngineState ( vehicle, false ) end end end addEvent ( "useFuel", true ) local usingFuel = function ( ) local vehicle = getPedOccupiedVehicle (localPlayer) local timerCheck = isTimer ( timer ) if vehicle then local engine = getVehicleEngineState ( vehicle ) if engine then local actualFuel = tonumber(getElementData ( vehicle, "fuel" )) or 0 if not timerCheck and actualFuel >= 1 then timer = setTimer ( fuelFunction, 2000, 0,vehicle) elseif actualFuel <= 0 then if timerCheck then killTimer ( timer ) end setVehicleEngineState ( vehicle, false ) end elseif timerCheck then killTimer ( timer ) end elseif timerCheck then killTimer ( timer ) end end addEventHandler ( "useFuel", root, usingFuel ) Edited August 2, 2013 by Guest
Mefisto_PL Posted August 2, 2013 Author Posted August 2, 2013 It's working, but when fuel = 0 and engine state is set to off then debugscript said "Bad argument @ 'killTimer' [Expected lua-timer at argument 1]"
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now