尝试索引零值(字段“ SpawnPoint)”

时间:2019-04-29 00:02:06

标签: indexing lua null

我为我的一个朋友创建了一个Fivem服务器,而我目前在车库方面遇到了问题。当前正在尝试索引“ spawnpoint”的nil值

            for i=1, #v.Vehicles, 1 do

                if GetDistanceBetweenCoords(coords,  v.Vehicles[i].Spawner.x,  v.Vehicles[i].Spawner.y,  v.Vehicles[i].Spawner.z,  true) < Config.MarkerSize.x then
                    isInMarker     = true
                    currentStation = k
                    currentPart    = 'VehicleSpawner'
                    currentPartNum = i
                end

                if GetDistanceBetweenCoords(coords,  v.Vehicles[i].SpawnPoint.x,  v.Vehicles[i].SpawnPoint.y,  v.Vehicles[i].SpawnPoint.z,  true) < Config.MarkerSize.x then
                    isInMarker     = true
                    currentStation = k
                    currentPart    = 'VehicleSpawnPoint'
                    currentPartNum = i
                end

            end

1 个答案:

答案 0 :(得分:1)

正如brianolive所说,似乎并非每个v.Vehicle都有SpawnPoint。例如,您可以通过证明SpawnPoint的存在来解决此问题。

if v.Vehicles[i].SpawnPoint and (
  GetDistanceBetweenCoords(
    coords, v.Vehicles[i].SpawnPoint.x, v.Vehicles[i].SpawnPoint.y,
    v.Vehicles[i].SpawnPoint.z, true
  ) < Config.MarkerSize.x
) then