A couple things for your code. Just general improvements, since it seems like your problem is fixed.
1: You don't need an else for an if, which you know because of the if args[2] then. However for some reason you have an else inside your for loop that does nothing.
2: Use locals, it's good practice and they're loaded faster than global variables which you have now. It's exceptionally easy, just have to add the word local when you first declare the variable. There are tricky rules that go with locals though.
I would check out this tutorial.3: Something that most people do, and enjoy reading code that's like this, is indenting. When you hit a new code block (a function, loop, if statement) you increase the indentation level, and when hitting the end of those, the indentation level is reduced. It helps and allows you to see if somethings encased in something else. Example:
local function foo()
print("Hello")
end
for 1,2 do
print("World") --#This isn't going to be printed when I run foo. I wanted that.
end
local function bar()
print("Hello there")
for 1,2 do
print("Everybody.")
end
end
Do you notice how you can see that the for loop is inside the function on the second one more easily than the first? This helps you figure out the structure to a point without having to go through line by line. And you can make sure that you have the proper number of ends.
That's basically it, however there's still always more to learn :P/>