Jump to content

Block Player Car Pushing


matin

Recommended Posts

hi guys i want when vehicle dont have driver player's Cant push it 
 

function carfreez()
	setTimer ( function()
	for _,veh in ipairs (getElementsByType("vehicle")) do
		if veh then
			if isVehicleEmpty(veh) then
		local speed = ( function( x, y, z ) return math.floor( math.sqrt( x*x + y*y + z*z ) * 155 ) end )( getElementVelocity( veh ) ) 
		if speed and speed < 2 then
			setElementFrozen(veh,true)
		end
		end
	end
	end
	end, 1000, 0 )
end
carfreez()

function addHelmetOnEnter ( theVehicle, seat, jacked )
    setElementFrozen(theVehicle,false)
end
addEventHandler ( "onPlayerVehicleEnter", getRootElement(), addHelmetOnEnter )

it worked but i want when any car want push another car and car dont have driver , car move. any idea ? 

Link to comment
  • Scripting Moderators
59 minutes ago, matin said:

hi guys i want when vehicle dont have driver player's Cant push it 
 


function carfreez()
	setTimer ( function()
	for _,veh in ipairs (getElementsByType("vehicle")) do
		if veh then
			if isVehicleEmpty(veh) then
		local speed = ( function( x, y, z ) return math.floor( math.sqrt( x*x + y*y + z*z ) * 155 ) end )( getElementVelocity( veh ) ) 
		if speed and speed < 2 then
			setElementFrozen(veh,true)
		end
		end
	end
	end
	end, 1000, 0 )
end
carfreez()

function addHelmetOnEnter ( theVehicle, seat, jacked )
    setElementFrozen(theVehicle,false)
end
addEventHandler ( "onPlayerVehicleEnter", getRootElement(), addHelmetOnEnter )

it worked but i want when any car want push another car and car dont have driver , car move. any idea ? 

Your code is badly optimized, i've fixed it. Should work, untested.

-- Useful function

function isVehicleEmpty(vehicle)
	if not isElement(vehicle) or getElementType(vehicle) ~= "vehicle" then
		return true
	end

	local passengers = getVehicleMaxPassengers(vehicle)
	if type(passengers) == "number" then
		for seat = 0, passengers do
			if getVehicleOccupant(vehicle, seat) then
				return false
			end
		end
	end
	return true
end

-- Unfroze when entering

function onPlayerVehicleEnter(vehicle, seat, jacked)
	if isElementFrozen(vehicle) then -- don't froze if element is frozen
		setElementFrozen(vehicle, false)
	end
end
addEventHandler("onPlayerVehicleEnter", getRootElement(), onPlayerVehicleEnter)

-- Froze on exit

function onPlayerVehicleExit(vehicle, seat, jacker, forced)
	if not isElementFrozen(vehicle) and isVehicleEmpty(vehicle) then -- don't froze if element is frozen, and be sure that vehicle is empty
		setElementFrozen(vehicle, true)
	end
end
addEventHandler("onPlayerVehicleExit", getRootElement(), onPlayerVehicleExit)

-- Check all vehicles on resource start

function onResourceStart()
	local vehicles = getElementsByType("vehicle")
	for i = 1, #vehicles do -- no need for ipairs, int loop faster
		local vehicle = vehicles[i] -- get vehicle
		if vehicle and isVehicleEmpty(vehicle) then -- check if vehicle is empty.
			if not isElementFrozen(vehicle) then -- don't froze if element is frozen
				setElementFrozen(vehicle, true)
			end
		end
	end
end
addEventHandler("onResourceStart", resourceRoot, onResourceStart)

-- Froze on vehicle spawn

function onVehicleRespawn(exploded)
	if isVehicleEmpty(source) then -- ditto
		if not isElementFrozen(source) then -- ditto
			setElementFrozen(source, true)
		end
	end
end
addEventHandler("onVehicleRespawn", getRootElement(), onVehicleRespawn) --[[for all vehicles, change to resourceRoot instead of getRootElement() - if it should happen for vehicles created only by this resource]] 

 

  • Sad 1
Link to comment
  • Scripting Moderators
17 hours ago, matin said:

@majqq it work but i want when any car accident with another car when another car don't have driver it should be unfreez and move like real life

 

Then you should use this.

https://wiki.multitheftauto.com/wiki/OnClientVehicleCollision

Get theHitElement (if it exists - it's parameter from event), check element type if is equal to vehicle, check if vehicle is frozen, and use:

https://wiki.multitheftauto.com/wiki/TriggerServerEvent

To unfroze vehicle for everyone, good luck. You should try to do that yourself, after that, when something will not work i wil try to help you with that.

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