Maybe I am wrong, but I think in this case it should be:
print(vars["name"..i])
Another way to do the loop is using the Lua "pairs" function and print both things, key and value, on each line (I think the order for the loop is random, or at least chaotic, but I am not sure). This would allow to have keys that are not of the form "nameX".