Okay, a few things:
For problem 1:
1) Is this on a server? If not, go into your mincraft folder, the save for whatever world this is (again, if it is not multiplayer), find the computer this is written on and copy the code to here.
2) Format. Everytime you have an if-block, put three spaces on the next line. For every end, take three spaces away.
while true do
--stuff
if stuff then
--stuff
elseif other stuff then
--more stuff
else
--even more stuff
end
end
Because the error you are getting is that you have one too many ends, most likely, and formatting is a very easy way to figure out where it is.
For the second problem.
You can use a for loop, if you want. That repeats a set of actions a definite number of times.
for i = 1, -- This sets up the start position, which can be useful for traversing a table, figuring out where a turtle is, etc.
endVar, -- This is your end position, usually a number higher than the initiator variable.
1 -- This is your incrementer. 1 is default, so if you are going up by one, you don't need this. For any other numbers (such as going downward) you will need it.
do -- Tells the computer where the for call ends
--Stuff goes here
end -- Make sure you cap everything. Just no too many things.
So, for what you want (say, 100 times), you would do this:
for i = 1, 100 do
-- Code
end
There are other ways, but this is the simplest route. Also. Go read the Lua reference document. Google Lua 5.1 and you'll find it.
EDIT: The guy beneath me brings up a loop style called recursive looping, which is a terrible idea for an infinite loop. Finite loops, fine, but not infinite. Any more than about two hundred iterations of it causes the program to stop due to reaching the sandbox's memory limit. A better infinite loop would be a while loop like so:
while true do
--Code
end
Which doesn't cause all sorts of nasty problems.