Jump to content




Help my code dont work :(


  • You cannot reply to this topic
3 replies to this topic

#1 cheekycharlie101

  • Members
  • 231 posts

Posted 17 September 2012 - 05:11 PM

ok so i made this simple turtle moving program
function move(distance)
for i = 1, distance do
turtle.forward()
end
end
please help me. i dont no why it wont work. what im trying to do is make it so when you type <programname> 5 it will move the turtle 5 blocks forward
but it doesent work D: please can someone tell me what im doing wrong?

#2 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 17 September 2012 - 05:21 PM

You forgot to turn the string you would get from read() to a number. Do this:
function move(distance)
for i = 1, tonumber(distance) do --tonumber() changes from string to a number
turtle.forward()
end
end


#3 GopherAtl

  • Members
  • 888 posts

Posted 17 September 2012 - 05:22 PM

you've written a function, which can only be called from lua, not from the command line. Running a file containing just this will only define the function "move(dist)", not actually call it. You can see that it's defined by running it, then running lua, and in lua typing "move(5)" which will cause your turtle to move forward 5 as expected.

Only code in a file that is not inside a function will be executed when you run the program from the command line, and a little extra work is involved in taking command-line parameters as well.

--this grabs all command-line parameters to an array
local params={...}

--pull out the first parameter and make it into a number (command line params are all strings normally)
local distance=tonumber(  params[1] )

--now your code from inside the function
for i = 1, distance do
  turtle.forward()
end

hope this helps!

#4 cheekycharlie101

  • Members
  • 231 posts

Posted 17 September 2012 - 08:28 PM

omg thanks guys soo much. i coudlent figure this out thanks again :)/> -Cheeky





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users