I just want to be able to use this function to return a true or false value. Here is the code:
local inRadius={} function checkTable(action) for i=1,#inRadius do wait() print(inRadius[i]) if action==inRadius[i] then return true else return false end end end
If the table is empty, it won't return anything, because it's not even searching anything.
local inRadius={} function checkTable(action) for i, v in pairs(inRadius) do wait() print(v) if action==v then return true end end return false -- if the table is empty, then it won't return anything. So we need to add the 'return false' after it goes through the table. This is basically an "if all else fails, return this" end
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") --//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") --For Testing