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

[Lua][Error] - Tower Construction Script

Started by Cynsye, 26 January 2013 - 08:35 PM
Cynsye #1
Posted 26 January 2013 - 09:35 PM
I am trying to code a tower construction program for a Turtle, but I am having an issue with the Turtle returning "turtle:18: Expected number" (the program nor the Turtle are named "turtle").

The error seems to be somewhere after line 28, as the section for restocking the turtle works fine, could anyone shed any light on what this error actually means, I feel like I am beating my head against a brick wall.

The code with updates given by people here can be found here: http://pastebin.com/X0zMYgNr

This is the original code:


shell.run("clear")
print("Note:")
print("- For now, please ensure that this turtle has sufficient fuel already registered")
print("- Please ensure that there is a suitable method of restocking this turtle via enderchest")
sleep(3)
shell.run("clear")
print("How many blocks high do you wish the tower to be?")
sleep(1)
local height = read()
shell.run("clear")
print("Construction in progress")

function RPlace()  -- Selecting random block, and placing it down in the world
local q = 0
repeat
local r = math.random(1, 12)
  if q == 15 then  -- Checking if the turtle needs to be restocked
   local q = 0
   print("Restocking")
   turtle.select(16)  -- Enderchest
   turtle.placeDown()
   local s = 0
	repeat
	turtle.suckDown()
   s = s + 1
   until s == 12
   turtle.digDown()
  end
q = q + 1
until turtle.getItemCount(r) > 1
turtle.select(r)
turtle.placeDown()
end

local k = 0  -- Level counter
repeat

local j = 0  -- Circle Start
repeat
  local i = 0
   repeat
   RPlace()  --SB1-5
   turtle.forward()
   i = i + 1
  until i == tonumber(5)

  turtle.turnRight()
  turtle.forward()
   RPlace()  -- CB1
  turtle.turnLeft()
  turtle.forward()
   RPlace()  -- CB2
  turtle.forward()
  turtle.turnRight()
  turtle.forward()
   RPlace()  -- CB3
  turtle.turnLeft()
  turtle.forward()
  turtle.turnRight()
  turtle.forward()
   RPlace()  -- CB4
  turtle.forward()
   RPlace()  -- CB5
  turtle.turnLeft()
  turtle.forward()
  turtle.turnRight()
  turtle.forward()

j = j + 1
until j == 4

turtle.up()
k = k + 1
until k == height

print("Construction complete!")

For reference, this is the sort of thing the program builds, from a simpler (working :P/>) version of the script.



Thanks in advance.
Lyqyd #2
Posted 27 January 2013 - 07:47 AM
Split into new topic.
remiX #3
Posted 27 January 2013 - 09:00 AM
Change
local height = read()
to
 local height = tonumber(read())

To convert the string into a number when entered with read()
Cynsye #4
Posted 28 January 2013 - 04:58 AM
Change
local height = read()
to
 local height = tonumber(read())

To convert the string into a number when entered with read()
Still giving the same error with that alteration I'm afraid.
Orwell #5
Posted 28 January 2013 - 05:21 AM
It's in the function RPlace:

repeat
  local r = math.random(1, 12)
  -- stuff ...
until turtle.getItemCount(r) > 1
turtle.select(r)
r has been defined within the loop only and you use it in turtle.select() after the loop…
Cynsye #6
Posted 28 January 2013 - 05:46 AM
It's in the function RPlace:

repeat
  local r = math.random(1, 12)
  -- stuff ...
until turtle.getItemCount(r) > 1
turtle.select(r)
r has been defined within the loop only and you use it in turtle.select() after the loop…
Thanks, I moved the slot selection within the loop and it works fine.

Always the simple things I miss :S