News: Welcome back to Bullworth! If you haven't already, you will need to reset your password..


Author Topic: Destroy car  (Read 1220 times)

0 Members and 1 Guest are viewing this topic.

Offline Ming

  • Full Member
  • ***
  • Posts: 154
  • Gender: Male
  • 抽刀斷水水更流,舉杯消愁愁更愁
    • View Profile
Destroy car
« on: March 14, 2022, 03:41:44 AM »
Code: [Select]
function main()
  repeat
    Wait(0)
  until SystemIsReady() and not AreaIsLoading()
 
  CreateThread("DestroyCar")
  CreateThread("F_BurningCars")
 
end

DestroyCar = function()
  PedSetFlag(gPlayer, 42, true)
  BurningCars = {}
  while true do
    local AllVehicles = VehicleFindInAreaXYZ(0, 0, 0, 999999)
    if type(AllVehicles) == "table" then
      for VHCNumber, Vehicle in ipairs(AllVehicles) do
       
        if VehicleIsValid(Vehicle) and not VehicleIsModel(Vehicle, 272) and not VehicleIsModel(Vehicle, 273) and not VehicleIsModel(Vehicle, 274) and not VehicleIsModel(Vehicle, 275) and not VehicleIsModel(Vehicle, 276) and not VehicleIsModel(Vehicle, 277) and not VehicleIsModel(Vehicle, 278) and not VehicleIsModel(Vehicle, 279) and not VehicleIsModel(Vehicle, 280) and not VehicleIsModel(Vehicle, 281) and not VehicleIsModel(Vehicle, 282) and not VehicleIsModel(Vehicle, 283) and not VehicleIsModel(Vehicle, 284) and not VehicleIsModel(Vehicle, 289) then
         
          if CarGetDamageNumber(Vehicle) == 4 and not IsInTable(BurningCars, Vehicle) then
           
            for i, v in ipairs(BurningCars) do
             
              VNumber = i + 1
             
            end
           
            BurningCars[VNumber] = {Vehicle, GetTimer()}
           
            local Effect1 = EffectCreate("CarDestroyed", VehicleGetPosXYZ(Vehicle))
        EffectSlowKill(Effect1, 0.5)
           
          end
         
        end
      end
    end
    Wait(0)
  end
end

F_BurningCars = function()
 
  while true do
   
    for i, BCarT in pairs(BurningCars) do
     
      if BCarT[1] ~= nil and GetTimer() - BCarT[2] >= 2500 then
        local Effect1 = EffectCreate("CarDestroyed", VehicleGetPosXYZ(BCarT[1]))
        EffectSlowKill(Effect1, 0.5)
        VehicleDelete(BCarT[1])
        BurningCars[i] = nil
      else
        local Effect2 = EffectCreate("GymFire", VehicleGetPosXYZ(BCarT[1]))
        EffectSlowKill(Effect2, 0.2)
      end
     
    end
   
    Wait(0)
  end
end

IsInTable = function(t, value)
  for i, v in pairs(t) do
    if v == value then
      do return true end
      break
    end
    return false
  end
end
Is IsInTable() wrong ?
How to fix this

Offline RBS ID

  • Jr. Member
  • **
  • Posts: 67
  • Gender: Male
  • I don't know.
    • View Profile
    • This is website title.
Re: Destroy car
« Reply #1 on: March 14, 2022, 12:06:01 PM »
1. You're trying to iterate an empty table.
2. Why not just use table.insert?
3. return  is also break the loop.
4. Yeah,  return false  placement in IsInTable function should be after  for loop. So, it'll check if the value in the table is match or not with the given value in argument, after checking process is complete, then just return false.

Code: [Select]

local burningCars = {}

function IsInTable(t, value)
  for k, v in pairs(t) do
    if v == value then
      return true
    end
  end
  return false
end

function DestroyCar()
  PedSetFlag(gPlayer, 42, true)
  while true do
    Wait(0)
    local allVehicles = VehicleFindInAreaXYZ(0, 0, 0, 999999)
    if type(allVehicles) == "table" then
      for i, veh in ipairs(allVehicles) do
        if VehicleIsValid(veh) and not VehicleIsModel(veh, 272) and not VehicleIsModel(veh, 273) and not VehicleIsModel(veh, 274) and not VehicleIsModel(veh, 275) and not VehicleIsModel(veh, 276) and not VehicleIsModel(veh, 277) and not VehicleIsModel(veh, 278) and not VehicleIsModel(veh, 279) and not VehicleIsModel(veh, 280) and not VehicleIsModel(veh, 281) and not VehicleIsModel(veh, 282) and not VehicleIsModel(veh, 283) and not VehicleIsModel(veh, 284) and not VehicleIsModel(veh, 289) then
          if CarGetDamageNumber(veh) >= 4 and not IsInTable(burningCars, veh) then
            --[[for i, v in ipairs(burningCars) do
             
              VNumber = i + 1
             
            end
            BurningCars[VNumber] = {Vehicle, GetTimer()}]]
            table.insert(burningCars, {veh, GetTimer()}
            local Effect1 = EffectCreate("CarDestroyed", VehicleGetPosXYZ(veh))
            EffectSlowKill(Effect1, 0.5)
          end
        end
      end
    end
  end
end

function F_BurningCars()
  while true do
    Wait(0)
    for i, BCarT in ipairs(burningCars) do
      if BCarT[1] ~= nil and GetTimer() - BCarT[2] >= 2500 then
        local Effect1 = EffectCreate("CarDestroyed", VehicleGetPosXYZ(BCarT[1]))
        EffectSlowKill(Effect1, 0.5)
        VehicleDelete(BCarT[1])
        burningCars[i] = nil
      else
        local Effect2 = EffectCreate("GymFire", VehicleGetPosXYZ(BCarT[1]))
        EffectSlowKill(Effect2, 0.2)
      end
    end
  end
end

function main()
  repeat
    Wait(0)
  until SystemIsReady()
  CreateThread("DestroyCar")
  CreateThread("F_BurningCars")
  repeat Wait(0) until not true
end


cmiiw

Offline Ming

  • Full Member
  • ***
  • Posts: 154
  • Gender: Male
  • 抽刀斷水水更流,舉杯消愁愁更愁
    • View Profile
Re: Destroy car
« Reply #2 on: April 01, 2022, 09:03:00 PM »
1. You're trying to iterate an empty table.
2. Why not just use table.insert?
3. return  is also break the loop.
4. Yeah,  return false  placement in IsInTable function should be after  for loop. So, it'll check if the value in the table is match or not with the given value in argument, after checking process is complete, then just return false.

Code: [Select]

local burningCars = {}

function IsInTable(t, value)
  for k, v in pairs(t) do
    if v == value then
      return true
    end
  end
  return false
end

function DestroyCar()
  PedSetFlag(gPlayer, 42, true)
  while true do
    Wait(0)
    local allVehicles = VehicleFindInAreaXYZ(0, 0, 0, 999999)
    if type(allVehicles) == "table" then
      for i, veh in ipairs(allVehicles) do
        if VehicleIsValid(veh) and not VehicleIsModel(veh, 272) and not VehicleIsModel(veh, 273) and not VehicleIsModel(veh, 274) and not VehicleIsModel(veh, 275) and not VehicleIsModel(veh, 276) and not VehicleIsModel(veh, 277) and not VehicleIsModel(veh, 278) and not VehicleIsModel(veh, 279) and not VehicleIsModel(veh, 280) and not VehicleIsModel(veh, 281) and not VehicleIsModel(veh, 282) and not VehicleIsModel(veh, 283) and not VehicleIsModel(veh, 284) and not VehicleIsModel(veh, 289) then
          if CarGetDamageNumber(veh) >= 4 and not IsInTable(burningCars, veh) then
            --[[for i, v in ipairs(burningCars) do
             
              VNumber = i + 1
             
            end
            BurningCars[VNumber] = {Vehicle, GetTimer()}]]
            table.insert(burningCars, {veh, GetTimer()}
            local Effect1 = EffectCreate("CarDestroyed", VehicleGetPosXYZ(veh))
            EffectSlowKill(Effect1, 0.5)
          end
        end
      end
    end
  end
end

function F_BurningCars()
  while true do
    Wait(0)
    for i, BCarT in ipairs(burningCars) do
      if BCarT[1] ~= nil and GetTimer() - BCarT[2] >= 2500 then
        local Effect1 = EffectCreate("CarDestroyed", VehicleGetPosXYZ(BCarT[1]))
        EffectSlowKill(Effect1, 0.5)
        VehicleDelete(BCarT[1])
        burningCars[i] = nil
      else
        local Effect2 = EffectCreate("GymFire", VehicleGetPosXYZ(BCarT[1]))
        EffectSlowKill(Effect2, 0.2)
      end
    end
  end
end

function main()
  repeat
    Wait(0)
  until SystemIsReady()
  CreateThread("DestroyCar")
  CreateThread("F_BurningCars")
  repeat Wait(0) until not true
end


cmiiw
Now I get it! Thank you