arraysluafivem

Trying to clear a array when all values are inserted on other array


So im trying to clear the array NewPassagers when all values inside passed to OnBusPassagers. Its for a FiveM script btw!

 if GetVehicleNumberOfPassengers(veh) > 1 then
              for i = 1, #NewPassagers, 1 do
                  table.insert(OnBusPassagers, NewPassagers)
              end
              NewPassagers = nil
          end

Solution

  • Maybe lets do a while, table.insert() and table.remove() do the job...

    local tab1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
    local tab2 = {}
    
    while #tab1 ~= 0 do
    -- Reverse order
    --  table.insert(tab2, table.remove(tab1))
    -- Same order
      table.insert(tab2, table.remove(tab1, 1))
    end
    
    return tab2
    

    ...table.remove({}[, pos]) removes last key/value by default without pos and returning that value for table.insert().