This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
pieiscool32's profile picture

[LUA]Problem with loops/while true do

Started by pieiscool32, 25 March 2012 - 05:32 PM
pieiscool32 #1
Posted 25 March 2012 - 07:32 PM
I keep getting an "end" expected at line 216, and i want to know why, because my code doesn't stop there and i think i maxed out the possible loops in a loop or something. Can someone take a look?
Link to pastebin here.
Turtle #2
Posted 25 March 2012 - 07:39 PM
Simple
You shouldn't add an end after ever elseif
what you did
if Bla then
elseif Blar then
end
elseif Blargh then
end

What you should do
if Bla then
elsif Blar then
elseif Blargh then
end –1 end
pieiscool32 #3
Posted 25 March 2012 - 08:15 PM
What you should do
if Bla then
elsif Blar then
elseif Blargh then
end –1 end

ok, but i want it to make it so if it failed to connect to try again, will the fix do that?
Wolvan #4
Posted 25 March 2012 - 08:33 PM
Yeah sure. If the if-clause is in the loop it just restarts the loop after checking and checks again
EatenAlive3 #5
Posted 25 March 2012 - 11:56 PM
Also, in addition to what they said, in the future it would help if you indented in an easier to read way. You indent like this:


while true do
	setSomething()
		 foo()
		 print("detonating in "..time)
	   setBar(5)

	 if y ~= z then
   while true do
		if world:ends() then
		x = getMagicNumber(bar)
		redstone.setOutput("back")
		else break end
	  sleep(2)
	  end
	   else break
	end
	sleep(1)
	end


When it should look something like this:

while true do
  setSomething()
  foo()
  print("detonating in "..time)
  setBar(5)
  if y ~= z then
	while true do
	  if world:ends() then
		x = getMagicNumber(bar)
		redstone.setOutput("back")
	  else
		break
	  end
	  sleep(2)
	end
  else break
  end
sleep(1)
end


The second version is the same code, just a lot more readable. You can check to see if you have too many ends or not enough.