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

Help with moving system

Started by ThePizzaBoy, 29 June 2013 - 01:02 PM
ThePizzaBoy #1
Posted 29 June 2013 - 03:02 PM
Title: Help with moving system

I made a little movement system for turtles and it works fine until it gets to the turn function. the error is that it is expecting 'then' on line 36 when it is clearly there i dont know what else to do.


function forward()
  print("How far do you want to go forward?")
  local i = read()
  for f = 1, i, 1 do
    turtle.forward()
  end
end

function up()
  print("How far do you want to go up?")
  local i = read()
  for u = 1, i, 1 do
    turtle.up()
  end
end

function back()
  print("How far do you want to go back?")
  local i = read()
  for b = 1, i, 1 do
    turtle.back()
  end
end

function down()
  print("How far do you want to go down?")
  local i = read()
  for d = 1, i, 1 do
    turtle.down()
  end
end

function turn()
  print("Do you want to turn right, left or none?")
  local input = read()
  if input = right then
    print("How many rotations to the right?")
    local i = read()
    for r = 1, i, 1 do
	  turtle.turnRight()
    end
  end
  if input = left then
    print("How many rotations to the left?")
    local i = read()
    for l = 1, i, 1 do
	  turtle.turnLeft()
    end
  end
  if input = none then
  end
end
turn()
forward()
up()
back()
down()
Lyqyd #2
Posted 29 June 2013 - 03:14 PM
Split into new topic.

== is the comparison operator, = is the assignment operator. Use the comparison operator when you're comparing two values in an if statement.
Apfeldstrudel #3
Posted 30 June 2013 - 09:26 AM
To clarify,

if x=17 then
  do stuff
end
This wont work, you are saying "if 17 then do this"- that sentence doesnt make sensw to humans… Nor to computers.

if x==17 then
  do stuff
end
This will work, you are now saying "if x is 17 then do this."
Engineer #4
Posted 30 June 2013 - 09:41 AM
read() returns a string, but you need a number for the for-loops.
With this snippet you keep asking until the string is a number:

local var = nil
repeat
   var = tonumber(read())
until var
Jappards #5
Posted 30 June 2013 - 09:46 AM
i corrected some things:
Spoiler

function forward()
  print("How far do you want to go forward?")
  local i = tonumber(read())
  for f = 1, i do--you don`t need the last 1.
	turtle.forward()
  end
end

function up()
  print("How far do you want to go up?")
  local i = tonumber(read())
  for u = 1, i do
	turtle.up()
  end
end

function back()
  print("How far do you want to go back?")
  local i = tonumber(read())
  for b = 1, i do
	turtle.back()
  end
end

function down()
  print("How far do you want to go down?")
  local i = tonumber(read())
  for d = 1, i do
	turtle.down()
  end
end

function turn()
  print("Do you want to turn right, left or none?")
  local input = read()
  if input == "right" then--[[the "right" needs to be in quotes, because this is text, text always needs to be a string, read converts that automaticly
	print("How many rotations to the right?")
	local i = tonumber(read())
	for r = 1, i do
	  turtle.turnRight()
	end
  end
  if input == "left" then
	print("How many rotations to the left?")
	local i = tonumber(read())
	for l = 1, i do
	  turtle.turnLeft()
	end
  end
  if input == "none" then--if you put an if statement inside a script, it needs to actually do something
   sleep(10)
  end[/size]
end
turn()
forward()
up()
back()
down()
this code should work, but it hasn`t been tested yet.
Pharap #6
Posted 30 June 2013 - 10:58 AM

if input = "none" then--if you put an if statement inside a script, it needs to actually do something
   sleep(10)
  end

More assigning instead of testing.