[Help][Lua] Trying to Loop through a table to check if an entity exists but it's failing due to my v not having a value

So long story short. I have 3 locations (Helipads) that I want to spawn helicopters on to. But I want to check the helipad first off. So if a DoesEntityExist() come back as true, then skip the helipad and cycle to the next one, if a vacant one is found. Spawn the helicopter, otherwise notify that the spawns are all blocked.

This is my current code, currently I’m trying to make it print a message, On the elseif, it prints all 3 coords of the helipads, whereas the string im trying to call is giving me this error:

"attempt to index a nil value (global ‘x’)

helipads = {
    {x = -2180.76, y = 3202.30, z = 35.00, heading = 330.79},
    {x = -2159.49, y = 3190.64, z = 35.00, heading = 326.20},
    {x = -2184.85, y = 3175.46, z = 35.00, heading = 330.63}
}

local landVehicleSpawnClearCheck = GetClosestVehicle(landVehicleSpawnLocation.x, landVehicleSpawnLocation.y, landVehicleSpawnLocation.z, 50.0, 0, 23) --23 checks when not inside a vehicle.

for k, v in pairs (helipads) do
    local helipadSpawnClearCheck = GetClosestVehicle(v.x, v.y, v.z, 15.0, 0, 12294)
    
    if DoesEntityExist(helipadSpawnClearCheck) then
        print("Helipad " ..v " Blocked")
    elseif not DoesEntityExist(helipadSpawnClearCheck) then
        print(v.x, v.y, v.z)
    end
end

I’ve tried reading general lua documentation on tables and loops and still got no further, I’ve tried naming the tables so that rather than it being a few arrays within the table, I put 1 = {x,y,z}, 2 ={x,y,z} but this errored as it seems you cannot assign an int to an array within a table?

Thanks in advance.

You’re trying to get a value of x from landVehicleSpawnLocation but landVehicleSpawnLocation isn’t a thing in your code, that’s what’s throwing the error. Also what do you even need the landVehicleSpawnClearCheck for? It’s not used anywhere. Also I would probably use StartShapeTestBox instead of GetClosestVehicle, shapetests are fast

1 Like

Ah apoligies, the landVehicleSpawnCheck is used elsewhere for ground based vehicles, that works fine. I don’t see where I’m calling that within the loop though?

the …v is from the value of the Helipads table is it not?

Ahh thanks, I’ll have a look into Shapetests :slight_smile:

Maybe try:

print("Helipad " ..k.. " Blocked") --k will return the index of "helipads" table, because printing "v" will show the table id...or some number related to the table itself, not the contents.

1 Like
helipads = {
    [1] = {x = -2180.76, y = 3202.30, z = 35.00, heading = 330.79},
    [2] = {x = -2159.49, y = 3190.64, z = 35.00, heading = 326.20},
    [3] = {x = -2184.85, y = 3175.46, z = 35.00, heading = 330.63}
}

helipads[1] = table
1 Like

Thanks, putting …k… was the right thing to do, So no need to add the [1], [2] to the helipad locations as it’s indexed correctly.

The reason why it worked now, is because at first you forgot to put the last .. at the end of the variable.

For future reference, this is how you can “connect” strings to variables:

local var = "Test"
print(var.. " 123")
print("123 " ..var)
print("123 "..var.." 456")
1 Like

Ahh thanks.

I don’t suppose I can ask you another question.

The values I have in that table, currently I’m trying to spawn a helicopter at the coords if the DoesEntityExists() check comeback false. But when trying to get the coords, it’s getting a nil value. Again I’m probably doing something stupid, but I need to get the coords of the free helipads. How would I output that from a table?

Thanks,

Can you share your code where the values come up as nil?

1 Like

No worries, sorted it now.

Thanks for the help though.

1 Like