Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why does this always say kills and return nil? [SOLVED]

Asked by 10 years ago

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

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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.

1
Thank you XtremeSpy 80 — 10y
Ad

Answer this question