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

Syntax error elseif

Started by NanoBob, 06 May 2013 - 09:13 AM
NanoBob #1
Posted 06 May 2013 - 11:13 AM
i get an error :bios:338: [string: "startup"]:123: syntax error

here's a bit of code near line 123


...
elseif
  l==i and k==(cmd1) then
  (cmd1e)
elseif                     -- This is line 123
  l==i and k==(cmd2) then
  (cmd2e)
elseif
...
I have no idea what caused this error message.
Dlcruz129 #2
Posted 06 May 2013 - 11:20 AM
Can we see your whole code please?
NanoBob #3
Posted 06 May 2013 - 11:23 AM
sure

Spoiler


if turtle then
print[[---------------------------------------
Welcome to RemoteTurtle V1.7
---------------------------------------]]
else
if term.isColor() then
term.setTextColor(colors.lime)
end
print[[---------------------------------------------------
Welcome to RemoteTurtle V1.7
---------------------------------------------------
]]
end
if term.isColor() then
term.setTextColor(colors.white)
end
repeat
  print("Does the program already exist on your device")
  q=read()
  if
	q==("yes")
  then
	sleep(0)
  else
	if
	  q==("no")
	then
	  os.setComputerLabel("not yet defined")
	  fs.copy("disk/startup","startup")
	  print("You can now safely break the device and place it somewhere else without program loss")

	  error()  
	 end
  end
until q==("yes") or q==("no")

print("")
repeat
  print("Do you want this device to transmit or receive?")
  p=read()
  if
	p==("receive")
  then

os.setComputerLabel("Remote Turtle")
print("")
shell.run("id")
rednet.open("right")
print("")
print("What Id Do You Want To Link To?")
l=tonumber(read())
repeat
i,k=rednet.receive()
z=turtle.getFuelLevel()
if
  z<=80 then
  shell.run("refuel")
end
if
  z<=80 then
  rednet.send(l,"p2")
if
  l==i and k==("w") then
  turtle.forward()
elseif
  l==i and k==("a") then
  turtle.turnLeft()
  turtle.forward()
elseif
  l==i and k==("s") then
  turtle.turnRight()
  turtle.turnRight()
  turtle.forward()
elseif
  l==i and k==("d") then
  turtle.turnRight()
  turtle.forward()
elseif
  l==i and k==("q") then
  turtle.up()
elseif
  l==i and k==("z") then
  turtle.down()
elseif
  l==i and k==("r") then
  shell.run("refuel")
elseif
  l==i and k==("x") then
  turtle.dig()
elseif
  l==i and k==("xup") then
  turtle.digUp()
elseif
  l==i and k==("xdown") then
  turtle.digDown()
elseif
  l==i and k==("turnleft") then
  turtle.turnLeft()
elseif
  l==i and k==("turnright") then
  turtle.turnRight()
elseif
  l==i and k==("clear") then
  os.setComputerLabel("")
elseif
  l==i and k==("f") then
  turtle.attack()
elseif
  l==i and k==("fup") then
  turtle.attackUp()
elseif
  l==i and k==("fdown") then
  turtle.attackDown()
elseif
  l==i and k==("reboot") then
  os.reboot()
elseif
  l==i and k==("shutdown") then
  os.shutdown()
elseif
  l==i and k==(cmd1) then
  (cmd1e)
elseif
  l==i and k==(cmd2) then
  (cmd2e)
elseif
  l==i and k==(cmd3) then
  (cmd3e)
elseif

  l==i and k==("AddCommand") then
  write("Which command do you want to change? 1/2/3")
  cwhat=read()
  if cwhat == ("1") then
	print("What do you want to type to to execute")
	cmd1=read()  
	print("What do you want to execute?")
	cmd1e=read()
  elseif
	chwat == ("2") then
	print("What do you want to type to execute?")
	cmd2=read()
	print("What do you want to execute?")
	cmd2e=read()
  elseif
	chwat == ("3") then
	print("What do you want to type to execute?")
	cmd3=read()
	print("What do you want to execute?")
	cmd3e=read()

  end

else
  sleep(0.2)
  print("Doesn't match")
  rednet.send(l,"p")
end
sleep(0.2)
rednet.send(l,"Matches")
until
l==i and k==("stop") or l==i and k==("clear")

elseif
	p==("transmit")
  then
	os.setComputerLabel("Remote Controller")
	print("")
	shell.run("id")
	print("")


	  if
		peripheral.getType("right") == "modem" then
		rednet.open("right")
	  elseif
		peripheral.getType("left") == "modem" then
		rednet.open("left")
	  elseif
		peripheral.getType("top") == "modem" then
		rednet.open("top")
	  elseif
		peripheral.getType("bottom") == "modem" then
		rednet.open("bottom")
	  elseif
		peripheral.getType("back") == "modem" then
		rednet.open("back")
	  elseif
		peripheral.getType("front") == "modem" then
		rednet.open("front")
	  end



	print("")

	print("What id would you like to connect to?")
	l=tonumber(read())

	print("")
	print("Type /list for a list of commands")
	print("")

	repeat
	  write("Command : ")
	  x=read()
	  if
		x==("/list")
	  then
		print("r --> refuel (This should be done automaticly)")
		print("w/a/s/d --> movement")
		print("q/z --> up/down")
		print("x --> dig")
		print("xup --> dig up")
		print("xdown --> dig down")
		print("turnleft/turnright --> turn left/ turn right")
		print("f --> attack")
		print("fup/fdown --> attacks up/down")
		print("shutdown --> Shuts down transmitter and receiver")
		print("reboot --> reboots transmitter and receiver")
		print("stop --> Stops using this program")
		print("clear --> Clear Memory after picking up")
	  else
		rednet.send(l,x)
		sleep(0.00000001)
		y,u=rednet.receive(4)
		if u==("p2") and y==l then
		  if term.isColor() then
			term.setTextColor(colors.red)
		  end
		  print("Out of coal and fuel")
		  if term.setTextColor then
			term.setTextColor(colors.white)
		  end			
		if u==("p") and y==l then
		  if term.isColor() then
			term.setTextColor(colors.red)
		  end
		  print("Incorrect Comand")
		  if term.isColor() then
			term.setTextColor(colors.white)
		  end
		elseif u==("Matches") and y==l then
		  if term.isColor() then
			term.setTextColor(colors.green)
		  end
		  print("Command Correct")
		  if term.isColor() then
			term.setTextColor(colors.white)
		  end
		end
	  end
	until
	  x==("clear") or x==("stop")
	end

	if
	  x==("clear")
	then
	  os.setComputerLabel("")
	end

  until
p==("receive") or p==("transmit")

pastebin:
Spoilerhttp://pastebin.com/zbfjczy6
NanoBob #4
Posted 06 May 2013 - 11:46 AM
I posted the code! Am i getting to impatient??
Kingdaro #5
Posted 06 May 2013 - 12:20 PM
This line and others like it:

(cmd1e)

This is the issue. What are you trying to do here?
NanoBob #6
Posted 06 May 2013 - 12:28 PM
I am trying to run an input
i have:

cmd1e=read()
I want somone to for example type turtle.suck() and it to run when the code reaches the (cmd1e)
Kingdaro #7
Posted 06 May 2013 - 12:30 PM
Right, well, that wasn't executed very well.

Instead, you will have the user type the functions without () or the turtle. at the beginning; e.g. "suck", "forward", "back", "right".

Then you would call it by replacing these lines:

(cmd1e)

With this:

turtle[cmd1e]()
NanoBob #8
Posted 06 May 2013 - 12:38 PM
ok, thanks!
LBPHacker #9
Posted 06 May 2013 - 12:40 PM
-snip-

Or a pcalled loadstring…
cmd1e = read()

-- some code here

local func, err = loadstring(cmd1e, "userinput") -- "userinput" will cause errors like "userinput:1: some message" if the given operation failed
if not func then
	-- this happens if the given operation was unloadable, eg. an if-end wasn't closed properly (compile error)
	-- err describes the error message returned by loadstring
	error(err)
end

setfenv(func, getfenv())
local ok, err = pcall(func) -- this will call the function that contains the given operation
if not ok then
	-- this happens when the given operation failed, eg. if it called turtle.Suck() (that's nil, of course) (runtime error)
	-- err describes the error message returned by pcall
	error(err)
end
Kingdaro #10
Posted 06 May 2013 - 04:45 PM
I would personally go with the much safer and less complicated solution.
NanoBob #11
Posted 06 May 2013 - 05:27 PM
I've put it in my program and it works, I will soon release my remote turtle V2.0 using this, thanks a lot.
http://www.computercraft.info/forums2/index.php?/topic/12508-remote-controlled-turtle/