Your code actually returns false if inRadius is empty, due to the fact that inRadius[i] would then be equivalent to nil, thus going to the 'false' return statement.
For proof, you can test your code with Lua's conditional ternary operator:
print(checkTable("s") and "yes" or "no")
There are a couple of issues with your code and let me point them out to you:
1.) If the table is empty, then your code will return false
2.) Your code only returns if the ***FIRST ***value in the table is homogeneous to action, and not every value.
3.) What this means is, is that if the first value in inRadius isn't action, then you are going to get it returned false, even if the proper action is in your table.
To fix this, we can simply return conditions after iteration:
local inRadius = {}
function checkTable(action)
for _,v in pairs(inRadius) do
if action==v then
return true
end
end
return false
end
print(checkTable("s") and "yes" or "no")
4 comments
Incorrect. The table will actually return the 'false' condition.
Yeah, I just moved the 'return false' portion to an 'else' function to fix that problem.
The return false is actually unnecessary, since without it the table would return nil, which is a 'falsy' value.
If you do an else return false, then it will return false on the first one if it doesn't match instead of searching the entire table.