When I run this code in my script, it prints Kills kills as the only things in the table and when I run checkStat("Points") it returns nil. It is part of a bigger script.
function checkStat(static) types={"Kills";"Deaths";"Points";"Demerits";"Promotion"} local check=static:lower() for i,v in pairs(types) do print(v) if v:lower():sub(1,check:len())==check then return v else return end end end
You don't want the else
clause inside your loop.
Because of the else
, this function always makes the decision based only on the first element it processes:
If static
matches "Kills", then return "Kills". Else, there was no match and return nothing.
You only know you should return nothing after the loop has been finished and all of the values have been checked.
And note a bare return
after the loop is superfluous since functions already implicitly return nil
.