Jump to content

Fuel system


TorNix~|nR

Recommended Posts

Hey guys, please can anyone help me? my fuelsystem

I want to disable the bikes on fuelsystem

bike ids: 510, 509, 481

help me please

Server-Side

-- Event that let the drive pay for the refueling
addEvent ( "onPlayerPaymentFuel", true )
addEventHandler ( "onPlayerPaymentFuel", root,
	function ( thePrice )
		takePlayerMoney ( source, thePrice )
	end
)

-- Exports to set vehicle fuel
function setVehicleFuel ( theVehicle, theFuel )
	if ( theVehicle ) and ( isElement( theVehicle ) ) and ( theFuel ) then
		setElementData ( theVehicle, "vehicleFuel", theFuel )
		return true
	else
		return false
	end
end

-- Exports to get vehicle fuel
function getVehicleFuel ( theVehicle )
	if ( theVehicle ) and ( isElement( theVehicle ) ) then
		return getElementData ( theVehicle, "vehicleFuel" )
	else
		return false
	end
end

 

Link to comment

Guys, I can't do it, please help me this is my full code

Client side

-- Some stats
local isPumping = false
local fuelPrice = 5
local currentMarker
local doEventHandlers

-- When the player enters a vehicle
addEventHandler ( "onClientVehicleEnter", root,
	function ( thePlayer, seat )
		if ( thePlayer == localPlayer ) and ( seat == 0 ) then
			if ( fuelTimer ) and ( isTimer( fuelTimer ) ) then killTimer( fuelTimer ) end
			if not ( getElementData ( source, "vehicleFuel" ) ) then
				setElementData ( source, "vehicleFuel", 100 )	
			elseif ( getElementData ( source, "vehicleFuel" ) <= 0 ) then	
				disableVehicleFunctions( source )
			else
				enableVehicleFunctions( source )
			end
			
			if ( doEventHandlers ) then
				addEventHandler ( "onClientVehicleExit", source, onVehicleExitDestroy, false )	
				addEventHandler ( "onClientElementDestroy", source, onVehicleExitDestroy, false )
				doEventHandlers = false
			end
			
			fuelTimer = setTimer ( onDecreaseFuel, 7000, 0 )
		end
	end
)

-- Disable all vehicle functions whenever the vehicle has no fuel left
function disableVehicleFunctions ( theVehicle )
	if ( fuelTimer ) and ( isTimer( fuelTimer ) ) then killTimer( fuelTimer ) end
	setVehicleEngineState( theVehicle, false )
	toggleControl ( "accelerate", false )
	toggleControl ( "brake_reverse", false )	
	outputChatBox( "This vehicle has stopped fuel, call a mechanic to refuel!", 0, 225, 0 )
end

-- Enable the vehicle functions again
function enableVehicleFunctions( theVehicle )
	setVehicleEngineState( theVehicle, true )
	toggleControl ( "accelerate", true )
	toggleControl ( "brake_reverse", true )	
end

-- When the resource starts
addEventHandler ( "onClientResourceStart", resourceRoot, 
	function ()
		if ( getPedOccupiedVehicle ( localPlayer ) ) and ( getVehicleOccupant ( getPedOccupiedVehicle ( localPlayer ), 0 ) == localPlayer ) then
			doEventHandlers = true
			local theVehicle = getPedOccupiedVehicle ( localPlayer )
			if not ( getElementData ( theVehicle, "vehicleFuel" ) ) then
				setElementData ( theVehicle, "vehicleFuel", 100 )	
			elseif ( getElementData ( theVehicle, "vehicleFuel" ) <= 0 ) then	
				disableVehicleFunctions( theVehicle )		
			end
			
			if ( doEventHandlers ) then
				addEventHandler ( "onClientVehicleExit", root, onVehicleExitDestroy, false )	
				addEventHandler ( "onClientElementDestroy", root, onVehicleExitDestroy, false )
				doEventHandlers = false
			end
				
			fuelTimer = setTimer ( onDecreaseFuel, 5000, 0 )
		end
	end
)

-- Function when a vehicle gets destroyed or when a player exit the vehicle
function onVehicleExitDestroy ( theVehicle )
	local theVehicle = theVehicle or source
	if ( fuelTimer ) and ( isTimer( fuelTimer ) ) then killTimer( fuelTimer ) end
	
	if ( getElementData ( theVehicle, "vehicleFuel" ) ) then
		setElementData ( theVehicle, "vehicleFuel", getElementData ( theVehicle, "vehicleFuel" ) )
	end
	
	unbindKey ( "K", "down", onRefuelVehicle )
	unbindKey ( "K", "up", onStopRefuelVehicle )
	isPumping = false
	
	if ( theVehicle ) then
		removeEventHandler ( "onClientVehicleExit", theVehicle, onVehicleExitDestroy, false )	
		removeEventHandler ( "onClientElementDestroy", theVehicle, onVehicleExitDestroy, false )
		doEventHandlers = true
	end
end

-- When the resource gets stopped
addEventHandler ( "onClientResourceStop", resourceRoot, 
	function ()
		if ( getPedOccupiedVehicle ( localPlayer ) ) and ( getVehicleOccupant ( getPedOccupiedVehicle ( localPlayer ), 0 ) == localPlayer ) then
			onVehicleExitDestroy ( getPedOccupiedVehicle ( localPlayer ) )
		end
	end	
)

-- Function that decreases the fuel
function onDecreaseFuel ()
	local theVehicle = getPedOccupiedVehicle ( localPlayer )
	if ( theVehicle ) then
		local theFuel = getElementData ( theVehicle, "vehicleFuel" )
		if ( theFuel ) and not ( isPumping ) and ( getVehicleEngineState ( theVehicle ) ) and ( theFuel > 0 ) and ( getVehicleOccupant ( getPedOccupiedVehicle ( localPlayer ), 0 ) == localPlayer ) then
			setElementData ( theVehicle, "vehicleFuel", theFuel - 1 )
			if ( getElementData ( theVehicle, "vehicleFuel" ) <= 0 ) then	
				disableVehicleFunctions( theVehicle )
			end	
		end	
	end
end

-- Get the vehicle speed
function getVehicleSpeed ( theVehicle, unit )
	if ( unit == nil ) then unit = 0 end
	if ( isElement( theVehicle ) ) then
		local x,y,z = getElementVelocity( theVehicle )
		if ( unit=="mph" or unit==1 or unit =='1' ) then
			return ( x^2 + y^2 + z^2 ) ^ 0.5 * 100
		else
			return ( x^2 + y^2 + z^2 ) ^ 0.5 * 1.61 * 100
		end
	else
		return false
	end
end

-- When the player hits a fuel marker
function onFuelPumpMarkerHit ( hitElement, matchingDimension, theMarker )
	local theMarker = theMarker or source
	if ( hitElement ) and ( getElementType ( hitElement ) == "player" ) and ( matchingDimension ) and ( getPedOccupiedVehicle( hitElement ) ) and ( isFuelMarker ( theMarker ) ) and ( getVehicleController( getPedOccupiedVehicle( hitElement ) ) == localPlayer ) then
		local theVehicle = getPedOccupiedVehicle( hitElement )
		if ( getElementData( theVehicle, "vehicleFuel" ) >= 100 ) then
			outputChatBox ( "Your vehicle has enough fuel!", 225, 0, 0 )
		else
			currentMarker = theMarker
			bindKey ( "K", "down", onRefuelVehicle )
			outputChatBox ( "Press 'K' if you want to refuel.", 0, 255, 0 )
		end
	end
end

-- When the player hits a fuel marker
function onFuelPumpMarkerLeave ( hitElement, matchingDimension )
	if ( hitElement == localPlayer ) then
		unbindKey ( "K", "down", onRefuelVehicle )
		unbindKey ( "K", "up", onStopRefuelVehicle )
		
		isPumping = false
	end
end

-- When the player press the space button to refuell
function onRefuelVehicle ()
	if ( getPedOccupiedVehicle( localPlayer ) ) then
		local theVehicle = getPedOccupiedVehicle( localPlayer )
		setElementFrozen ( theVehicle, true )		
		if ( getVehicleSpeed ( theVehicle, "kmh" ) >= 1 ) then		
			outputChatBox ( "Please bring the vehicle to a complete stop!", 225, 0, 0 )
		elseif ( getPlayerMoney() < fuelPrice ) then
			outputChatBox ( "You do not have enough money, the price is $" .. fuelPrice .." per litre!", 255, 0, 0 )
		else
			local oldFuel = math.floor( getElementData ( theVehicle, "vehicleFuel" ) )
			setTimer ( onRefillVehicle, 250, 1, theVehicle, oldFuel )
			outputChatBox ( "Your vehicle is being filled, please wait...", 0, 255, 0 )
			
			isPumping = true
			
			unbindKey ( "K", "down", onRefuelVehicle )
			bindKey ( "K", "up", onStopRefuelVehicle )
		end
	else
		onStopRefuelVehicle ()
	end
end








-- Actualy refill the vehicle
function onRefillVehicle( theVehicle, oldFuel, price )
	if ( theVehicle ) and ( oldFuel ) then
		local theFuel = tonumber( oldFuel )
		local thePrice = tonumber( price ) or 0
		if not ( getKeyState ( "K" ) ) then
			onStopRefuelVehicle ( theFuel, thePrice, theVehicle )
		elseif ( getPlayerMoney() < fuelPrice ) and not ( getElementData( theVehicle, "vehicleOccupation" ) ) then
			outputChatBox ( "You do not have enough money to continue, the price is $" .. fuelPrice .." per litre!", 255, 0, 0 )
			onStopRefuelVehicle ( theFuel, thePrice, theVehicle )
		elseif ( oldFuel >= 100 ) then
			onStopRefuelVehicle ( theFuel, thePrice, theVehicle )
		else
			theFuel = math.floor( theFuel + 1 )
			thePrice = math.floor( thePrice + 5 )
			setTimer ( onRefillVehicle, 250, 1, theVehicle, theFuel, thePrice )
			setElementData ( theVehicle, "vehicleFuel", theFuel )
		end
	end
end

-- When the player stops pressing space or stop fuel
function onStopRefuelVehicle ( theFuel, thePrice, theVehicle )
	unbindKey ( "K", "up", onStopRefuelVehicle )	
	isPumping = false
		local theVehicle = getPedOccupiedVehicle( localPlayer )
	
		local newFreezeStatus = not currentFreezeStatus		
		setElementFrozen ( theVehicle, false )			
	
	if ( theFuel ) and ( thePrice ) and not ( tostring( theFuel ) == "space" ) then
		if ( tonumber( theFuel ) < 100 ) then bindKey ( "K", "down", onRefuelVehicle ) outputChatBox ( "Press 'K' if you want to refuel more.", 225, 0, 0 ) end
		if ( theVehicle ) and ( getElementData( theVehicle, "vehicleOccupation" ) ) then 
			outputChatBox ( "The company, he works for pays its fuel, lucky bastard.", 0, 255, 0 )
		else
			triggerServerEvent ( "onPlayerPaymentFuel", localPlayer, thePrice )
			if ( thePrice ) then outputChatBox ( "You paid $" .. thePrice .. " for recharging!", 0, 255, 0 ) end
		end
	end
end

 

Server side

-- Event that let the drive pay for the refueling
addEvent ( "onPlayerPaymentFuel", true )
addEventHandler ( "onPlayerPaymentFuel", root,
	function ( thePrice )
		takePlayerMoney ( source, thePrice )
	end
)

-- Exports to set vehicle fuel
function setVehicleFuel ( theVehicle, theFuel )
	if ( theVehicle ) and ( isElement( theVehicle ) ) and ( theFuel ) then
		setElementData ( theVehicle, "vehicleFuel", theFuel )
		return true
	else
		return false
	end
end

-- Exports to get vehicle fuel
function getVehicleFuel ( theVehicle )
	if ( theVehicle ) and ( isElement( theVehicle ) ) then
		return getElementData ( theVehicle, "vehicleFuel" )
	else
		return false
	end
end

 

Help me please guys

Link to comment
  • Moderators

Try this:

-- Some stats
local isPumping = false
local fuelPrice = 5
local currentMarker
local doEventHandlers

local disabledVehs = {
	[510] = true, [509] = true, [481] = true
}

-- When the player enters a vehicle
addEventHandler ( "onClientVehicleEnter", root,
	function ( thePlayer, seat )
		if ( thePlayer == localPlayer ) and ( seat == 0 ) and disabledVehs[getElementModel(source)] ~= true then
			if ( fuelTimer ) and ( isTimer( fuelTimer ) ) then killTimer( fuelTimer ) end
			if not ( getElementData ( source, "vehicleFuel" ) ) then
				setElementData ( source, "vehicleFuel", 100 )	
			elseif ( getElementData ( source, "vehicleFuel" ) <= 0 ) then	
				disableVehicleFunctions( source )
			else
				enableVehicleFunctions( source )
			end
			
			if ( doEventHandlers ) then
				addEventHandler ( "onClientVehicleExit", source, onVehicleExitDestroy, false )	
				addEventHandler ( "onClientElementDestroy", source, onVehicleExitDestroy, false )
				doEventHandlers = false
			end
			
			fuelTimer = setTimer ( onDecreaseFuel, 7000, 0 )
		end
	end
)

-- Disable all vehicle functions whenever the vehicle has no fuel left
function disableVehicleFunctions ( theVehicle )
	if ( fuelTimer ) and ( isTimer( fuelTimer ) ) then killTimer( fuelTimer ) end
	setVehicleEngineState( theVehicle, false )
	toggleControl ( "accelerate", false )
	toggleControl ( "brake_reverse", false )	
	outputChatBox( "This vehicle has stopped fuel, call a mechanic to refuel!", 0, 225, 0 )
end

-- Enable the vehicle functions again
function enableVehicleFunctions( theVehicle )
	setVehicleEngineState( theVehicle, true )
	toggleControl ( "accelerate", true )
	toggleControl ( "brake_reverse", true )	
end

-- When the resource starts
addEventHandler ( "onClientResourceStart", resourceRoot, 
	function ()
		local vehicle = getPedOccupiedVehicle ( localPlayer )	
		if ( vehicle ) and ( getVehicleOccupant ( vehicle, 0 ) == localPlayer ) and disabledVehs[getElementModel(vehicle)] ~= true then
			doEventHandlers = true
			if not ( getElementData ( vehicle, "vehicleFuel" ) ) then
				setElementData ( vehicle, "vehicleFuel", 100 )	
			elseif ( getElementData ( vehicle, "vehicleFuel" ) <= 0 ) then	
				disableVehicleFunctions( vehicle )		
			end
			
			if ( doEventHandlers ) then
				addEventHandler ( "onClientVehicleExit", root, onVehicleExitDestroy, false )	
				addEventHandler ( "onClientElementDestroy", root, onVehicleExitDestroy, false )
				doEventHandlers = false
			end
				
			fuelTimer = setTimer ( onDecreaseFuel, 5000, 0 )
		end
	end
)

-- Function when a vehicle gets destroyed or when a player exit the vehicle
function onVehicleExitDestroy ( theVehicle )
	local theVehicle = theVehicle or source
	if ( fuelTimer ) and ( isTimer( fuelTimer ) ) then killTimer( fuelTimer ) end
	
	if ( getElementData ( theVehicle, "vehicleFuel" ) ) then
		setElementData ( theVehicle, "vehicleFuel", getElementData ( theVehicle, "vehicleFuel" ) )
	end
	
	unbindKey ( "K", "down", onRefuelVehicle )
	unbindKey ( "K", "up", onStopRefuelVehicle )
	isPumping = false
	
	if ( theVehicle ) then
		removeEventHandler ( "onClientVehicleExit", theVehicle, onVehicleExitDestroy, false )	
		removeEventHandler ( "onClientElementDestroy", theVehicle, onVehicleExitDestroy, false )
		doEventHandlers = true
	end
end

-- When the resource gets stopped
addEventHandler ( "onClientResourceStop", resourceRoot, 
	function ()
		local vehicle = getPedOccupiedVehicle ( localPlayer )
		if ( vehicle ) and ( getVehicleOccupant ( vehicle, 0 ) == localPlayer ) and disabledVehs[getElementModel(vehicle)] ~= true then
			onVehicleExitDestroy ( vehicle )
		end
	end	
)

-- Function that decreases the fuel
function onDecreaseFuel ()
	local theVehicle = getPedOccupiedVehicle ( localPlayer )
	if ( theVehicle ) then
		local theFuel = getElementData ( theVehicle, "vehicleFuel" )
		if ( theFuel ) and not ( isPumping ) and ( getVehicleEngineState ( theVehicle ) ) and ( theFuel > 0 ) and ( getVehicleOccupant ( getPedOccupiedVehicle ( localPlayer ), 0 ) == localPlayer ) then
			setElementData ( theVehicle, "vehicleFuel", theFuel - 1 )
			if ( getElementData ( theVehicle, "vehicleFuel" ) <= 0 ) then	
				disableVehicleFunctions( theVehicle )
			end	
		end	
	end
end

-- Get the vehicle speed
function getVehicleSpeed ( theVehicle, unit )
	if ( unit == nil ) then unit = 0 end
	if ( isElement( theVehicle ) ) then
		local x,y,z = getElementVelocity( theVehicle )
		if ( unit=="mph" or unit==1 or unit =='1' ) then
			return ( x^2 + y^2 + z^2 ) ^ 0.5 * 100
		else
			return ( x^2 + y^2 + z^2 ) ^ 0.5 * 1.61 * 100
		end
	else
		return false
	end
end

-- When the player hits a fuel marker
function onFuelPumpMarkerHit ( hitElement, matchingDimension, theMarker )
	if  hitElement == localPlayer and isPedInVehicle(localPlayer) then
		local veh = getPedOccupiedVehicle( localPlayer )
		if veh and disabledVehs[getElementModel(veh)] then
			return
		end
	end
	local theMarker = theMarker or source
	if ( hitElement ) and ( getElementType ( hitElement ) == "player" ) and ( matchingDimension ) and ( getPedOccupiedVehicle( hitElement ) ) and ( isFuelMarker ( theMarker ) ) and ( getVehicleController( getPedOccupiedVehicle( hitElement ) ) == localPlayer ) then
		local theVehicle = getPedOccupiedVehicle( hitElement )
		
		if ( getElementData( theVehicle, "vehicleFuel" ) >= 100 ) then
			outputChatBox ( "Your vehicle has enough fuel!", 225, 0, 0 )
		else
			currentMarker = theMarker
			bindKey ( "K", "down", onRefuelVehicle )
			outputChatBox ( "Press 'K' if you want to refuel.", 0, 255, 0 )
		end
	end
end

-- When the player hits a fuel marker
function onFuelPumpMarkerLeave ( hitElement, matchingDimension )
	if  hitElement == localPlayer and isPedInVehicle(localPlayer) then
		local veh = getPedOccupiedVehicle( localPlayer )
		if veh and disabledVehs[getElementModel(veh)] then
			return
		end
	end
	if ( hitElement == localPlayer ) and theVehicle and not isPedInVehicle(localPlayer) then
		unbindKey ( "K", "down", onRefuelVehicle )
		unbindKey ( "K", "up", onStopRefuelVehicle )
		
		isPumping = false
	end
end

-- When the player press the space button to refuell
function onRefuelVehicle ()
	local veh = getPedOccupiedVehicle( localPlayer )
	if ( veh ) and disabledVehs[getElementModel(veh)] ~= true then
		local theVehicle = getPedOccupiedVehicle( localPlayer )
		setElementFrozen ( theVehicle, true )		
		if ( getVehicleSpeed ( theVehicle, "kmh" ) >= 1 ) then		
			outputChatBox ( "Please bring the vehicle to a complete stop!", 225, 0, 0 )
		elseif ( getPlayerMoney() < fuelPrice ) then
			outputChatBox ( "You do not have enough money, the price is $" .. fuelPrice .." per litre!", 255, 0, 0 )
		else
			local oldFuel = math.floor( getElementData ( theVehicle, "vehicleFuel" ) )
			setTimer ( onRefillVehicle, 250, 1, theVehicle, oldFuel )
			outputChatBox ( "Your vehicle is being filled, please wait...", 0, 255, 0 )
			
			isPumping = true
			
			unbindKey ( "K", "down", onRefuelVehicle )
			bindKey ( "K", "up", onStopRefuelVehicle )
		end
	else
		onStopRefuelVehicle ()
	end
end








-- Actualy refill the vehicle
function onRefillVehicle( theVehicle, oldFuel, price )
	if ( theVehicle ) and ( oldFuel ) then
		local theFuel = tonumber( oldFuel )
		local thePrice = tonumber( price ) or 0
		if not ( getKeyState ( "K" ) ) then
			onStopRefuelVehicle ( theFuel, thePrice, theVehicle )
		elseif ( getPlayerMoney() < fuelPrice ) and not ( getElementData( theVehicle, "vehicleOccupation" ) ) then
			outputChatBox ( "You do not have enough money to continue, the price is $" .. fuelPrice .." per litre!", 255, 0, 0 )
			onStopRefuelVehicle ( theFuel, thePrice, theVehicle )
		elseif ( oldFuel >= 100 ) then
			onStopRefuelVehicle ( theFuel, thePrice, theVehicle )
		else
			theFuel = math.floor( theFuel + 1 )
			thePrice = math.floor( thePrice + 5 )
			setTimer ( onRefillVehicle, 250, 1, theVehicle, theFuel, thePrice )
			setElementData ( theVehicle, "vehicleFuel", theFuel )
		end
	end
end

-- When the player stops pressing space or stop fuel
function onStopRefuelVehicle ( theFuel, thePrice, theVehicle )
	unbindKey ( "K", "up", onStopRefuelVehicle )	
	isPumping = false
		local theVehicle = getPedOccupiedVehicle( localPlayer )
	
		local newFreezeStatus = not currentFreezeStatus		
		setElementFrozen ( theVehicle, false )			
	
	if ( theFuel ) and ( thePrice ) and not ( tostring( theFuel ) == "space" ) then
		if ( tonumber( theFuel ) < 100 ) then bindKey ( "K", "down", onRefuelVehicle ) outputChatBox ( "Press 'K' if you want to refuel more.", 225, 0, 0 ) end
		if ( theVehicle ) and ( getElementData( theVehicle, "vehicleOccupation" ) ) then 
			outputChatBox ( "The company, he works for pays its fuel, lucky bastard.", 0, 255, 0 )
		else
			triggerServerEvent ( "onPlayerPaymentFuel", localPlayer, thePrice )
			if ( thePrice ) then outputChatBox ( "You paid $" .. thePrice .. " for recharging!", 0, 255, 0 ) end
		end
	end
end

 

Edited by DNL291
  • Thanks 1
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...