Jump to content

Vehicle Car


iLimix

Recommended Posts

Posted
How can I remove vehicles where no players are sitting inside
		Cars[1] = createVehicle(DerbyVehicleID,-1586.494140625, 139.462890625, -11.316893577576,2.5323486328125, 359.22546386719, 135.25817871094)
		Cars[2] = createVehicle(DerbyVehicleID,-1589.765625, 142.4208984375, -11.316893577576,2.52685546875, 359.21447753906, 135.59875488281)
		Cars[3] = createVehicle(DerbyVehicleID,-1589.765625, 142.4208984375, -11.316893577576,2.52685546875, 359.21447753906, 135.59875488281)
		Cars[4] = createVehicle(DerbyVehicleID,-1589.765625, 142.4208984375, -11.316893577576,2.52685546875, 359.21447753906, 135.59875488281)
		Cars[5] = createVehicle(DerbyVehicleID,-1589.765625, 142.4208984375, -11.316893577576,2.52685546875, 359.21447753906, 135.59875488281)
		Cars[6] = createVehicle(DerbyVehicleID,-1589.765625, 142.4208984375, -11.316893577576,2.52685546875, 359.21447753906, 135.59875488281)
		Cars[7] = createVehicle(DerbyVehicleID,-1589.765625, 142.4208984375, -11.316893577576,2.52685546875, 359.21447753906, 135.59875488281)
		Cars[8] = createVehicle(DerbyVehicleID,-1589.765625, 142.4208984375, -11.316893577576,2.52685546875, 359.21447753906, 135.59875488281)

for example

There are 8 cars created only 6 are used how can I delete the last 2?
Posted (edited)
1 minute ago, NeXuS™ said:

You'll have to cycle through the cars and check if they have any passengers.

		for i=1, #Cars do
			vehicle = Cars[i]
			destroyElement( vehicle)
		end

like this?

Edited by iLimix
Posted (edited)
		for i=1, #Cars do
			vehicle = Cars[i]
			if getElementType(element) == player then
				if isPedInVehicle(element) then
					return
				else
					destroyElement( vehicle)
				end
			end
		end

But dont work ...

Edited by iLimix
Posted
5 minutes ago, NeXuS™ said:

Because this is not the right way to do it. You'll have to use getVehicleOccupants fuctions.

		if getPedOccupiedVehicle(player) then
			for i=1, #Cars do
				vehicle = Cars[i]
				destroyElement( vehicle)
			end
		end

Omg im idiot
Now he destroy but he destroy all Cars

Posted

Because this is still not the right way. Check the example on the WIKI page. Thats the way you can get the occupant count of the vehicle. And you can check if the count is greater than 0 (in which case, the car is occupied).

Posted
17 minutes ago, iLimix said:

		if getPedOccupiedVehicle(player) then			for i=1, #Cars do				vehicle = Cars[i]				destroyElement( vehicle)			end		end

Omg im idiot
Now he destroy but he destroy all Cars

for i= 7, 8 do
	local v = Cars[i]
	if isElement( v ) then 
		destroyElement(v)
		Cars[i] = nil
	end
end


 

Posted
Just now, MR.S3D said:

for i= 7, 8 do
	local v = Cars[i]
	if isElement( v ) then 
		destroyElement(v)
		Cars[i] = nil
	end
end


 

Thx but he Delete now 7 and 8 but when User user Cars 4 5 6 7 8 so i must delete then 1 2 3

Posted (edited)

As I said @iLimix, you'll have to use the function which I provided you earlier (getVehicleOccupants), use the given example on the wiki page to count the players inside the vehicle and check if the vehicle has 0 passengers (which means the vehicle is unused), and then delete it.

Edited by NeXuS™
  • Thanks 1
Posted
-- something like that maybe.
for i = 1, #Cars do
  local vehicle = Cars[i]
  local occupants = getVehicleOccupants(vehicle)
   if #occupants == 0 then
    destroyElement(vehicle)
   end
end

 

Posted (edited)

No, i sit in 1 Car and make "/Derby" and my car destroyed

local Derby_Event = false
local DerbyVehicleID = 428
local DerbyCars = {}

function DerbyEvent (player)
	if Derby_Event == false then
		Derby_Event = true
			setTimer ( function() 
			--1.Seite
			DerbyCars[1] = createVehicle(DerbyVehicleID,-1586.494140625, 139.462890625, -11.316893577576,2.5323486328125, 359.22546386719, 135.25817871094)
			DerbyCars[2] = createVehicle(DerbyVehicleID,-1589.765625, 142.4208984375, -11.316893577576,2.52685546875, 359.21447753906, 135.59875488281)
			DerbyCars[3] = createVehicle(DerbyVehicleID,-1592.1298828125, 144.771484375, -11.210712432861,2.52685546875, 359.21447753906, 135.59875488281)
			DerbyCars[4] = createVehicle(DerbyVehicleID,-1594.3203125, 147.6630859375, -11.191324234009,2.52685546875, 359.21447753906, 135.59875488281)
			DerbyCars[5] = createVehicle(DerbyVehicleID,-1598.3427734375, 149.5263671875, -11.249254226685,2.52685546875, 359.21447753906, 135.59875488281)
			DerbyCars[6] = createVehicle(DerbyVehicleID,-1600.6279296875, 153.0439453125, -11.488798141479,2.52685546875, 359.21447753906, 135.59875488281)
			DerbyCars[7] = createVehicle(DerbyVehicleID,-1604.56640625, 155.943359375, -11.481171607971,359.64294433594, 358.49487304688, 152.11669921875)
			DerbyCars[8] = createVehicle(DerbyVehicleID,-1583.814453125, 136.40625, -11.341559410095,2.0489501953125, 359.3408203125, 134.76379394531)
			--2. Seite
			DerbyCars[9] = createVehicle(DerbyVehicleID,-1672.3291015625, 57.30078125, -11.527992248535,359.53308105469, 359.99450683594, 316.26892089844)
			DerbyCars[10] = createVehicle(DerbyVehicleID,-1674.52734375, 59.5712890625, -11.549736022949,359.58801269531, 359.99450683594, 317.12585449219)
			DerbyCars[11] = createVehicle(DerbyVehicleID,-1676.1181640625, 61.314453125, -11.526648521423,359.53308105469, 359.99450683594, 314.15405273438)
			DerbyCars[12] = createVehicle(DerbyVehicleID,-1678.794921875, 62.8623046875, -11.528448104858,359.53857421875, 359.99450683594, 317.27966308594)
			DerbyCars[13] = createVehicle(DerbyVehicleID,-1681.04296875, 65.0771484375, -11.527297973633,359.53308105469, 0.0054931640625, 314.00573730469)
			DerbyCars[14] = createVehicle(DerbyVehicleID,-1683.6123046875, 67.412109375, -11.352535247803,359.53308105469, 0.0054931640625, 314.00573730469)
			end, 5000, 1 )
	else
			for i = 1, #DerbyCars do
				local vehicle = DerbyCars[i]
				local occupants = getVehicleOccupants(vehicle)
				if #occupants == 0 then
					destroyElement(vehicle)
				end
			end
		Derby_Event = false
	end

end
addCommandHandler("Derby",DerbyEvent)

 

Edited by iLimix
Posted (edited)

I hope this code helps, the first example is using it to destroy all cars that are empty and the second example is destroying all cars that are empty with only specific data, in case you don't want certain vehicles destroyed. You may find unnecessary things within this code, such as so many if statements, but I like to run these 'checks' to get rid of any possible errors or warnings within the script, it makes everything run smoothly.

Spoiler

for i,v in pairs(getElementsByType('vehicle')) do
  local occupants = getVehicleOccupants(v)
  if #occupants < 1 then
    destroyElement(v)
  end
end
-- This will destroy all empty vehicles, if you wish to only destroy certian ones u may set their element data and then destroy them like so.

function setVehicleData(vehicle,dataname,value)
  if vehicle and dataname and value then
    if getElementType(vehicle) == 'vehicle' then
      if tostring(dataname) and tostring(value) then
        setElementData(vehicle,dataname,value)
      end
    end
  end
end

-- example of using this below.
function cmdVD(player,cmd,data,value)
  if data and value then
    if tostring(data) and tostring(value) then
      local vehicle = getPedOccupiedVehicle(player)
      if vehicle then
        setVehicleData(vehicle,data,value)
      end
    end
  end
end
addCommandHandler('setdata',cmdVD) -- Then run /setdata type destroypls

function destroyElementVehicles(dataname,value)
  for i,v in pairs(getElementsByType('vehicle')) do
    if tostring(dataname) and tostring(value) then
      if getElementData(v,dataname) == value then
        destroyElement(v)
      end
    end
  end
end
addCommandHandler('destroycars',destroyElementVehicles) -- you would run /destroycars type destroypls
-- or use a timer to set it off, or trigger it with a function trigger.
-- this is a timer
setTimer(function()
    destroyElementVehicles('type','destroypls')
end,1000,1)-- use 0 as the last value to run this constant.
-- and this is a function trigger (This launches when the script starts.
destroyElementVehicles('type','destroypls')

 

 

Edited by shay103
  • Thanks 1

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