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

turtle make a platform: square:3: attempt to concatenate string and nil

Started by thijs1444, 17 August 2013 - 01:05 AM
thijs1444 #1
Posted 17 August 2013 - 03:05 AM
turtle make a platform

i have a problem with the next code. i get the error square:3: attempt to concatenate string and nil. i don't know what i did wrong.
local tArgs = (...)
turtle.forward()
print(tArgs[1]..":"..tArgs[2])
x = tonumber(tArgs[1])
y = tonumber(tArgs[2])
turtle.placeDown()
for j = 1, y do
  for i = 1, x-1 do
	 turtle.forward()
	 turtle.placeDown()
  end

  if j ~= y then
   if j % 2 == 0 then
	turtle.turnRight()
	turtle.forward()
	turtle.placeDown()
	turtle.turnRight()
   else
	turtle.turnLeft()
	turtle.forward()
	turtle.placeDown()
	turtle.turnLeft()
   end
  end
end

the turtle should lay a platform with size i type in when i type square (number) (number).
Bubba #2
Posted 17 August 2013 - 10:49 AM
Your problem is on line 1 where you have this:

local tArgs = (...)

The issue is with the parentheses. In order to make tArgs hold a table of arguments, you have to use brackets like so:

local tArgs = {...}
thijs1444 #3
Posted 17 August 2013 - 12:08 PM
thx for your reaction. it runs now. but i got a other problem: if he is out of stuff in his first slot, he don't take the stuff of his second slot. someone got a idea how i can fix it. because i don't know how i can set that in this code.
DaGamer12345 #4
Posted 17 August 2013 - 01:18 PM
You could do this:

local slot = 1 --Sets variable "slot" to 1
while turtle.getItemCount(slot) == 0 do --Executes following code if the current slot (and subsequent slots) are empty
local slot = slot + 1 --Raises variable "slot" by 1
if slot > 16 then --Executes following code if "slot" is 17+ (if this was not there, the program would error at one point)
  local slot = 1 --Resets "slot" to 1
end
turtle.select(slot) --Selects inventory slot specified by variable "slot"
end
Put this before the first turtle.placeDown()

Also, you should put this at the beginning of your code:

local function usage()
 print("Usage:")
 print("square <length> <width>")
end

if #tArgs ~= 2 then
 usage()
end
This will prevent the program from running if not enough (or too many) arguments are given
thijs1444 #5
Posted 17 August 2013 - 02:24 PM
You could do this:

local slot = 1 --Sets variable "slot" to 1
while turtle.getItemCount(slot) == 0 do --Executes following code if the current slot (and subsequent slots) are empty
local slot = slot + 1 --Raises variable "slot" by 1
if slot > 16 then --Executes following code if "slot" is 17+ (if this was not there, the program would error at one point)
  local slot = 1 --Resets "slot" to 1
end
turtle.select(slot) --Selects inventory slot specified by variable "slot"
end
Put this before the first turtle.placeDown()
this don't work at me. i just set it before the first turtle.placeDown() but it don't work.

local tArgs = {...}
turtle.forward()
print(tArgs[1]..":"..tArgs[2])
x = tonumber(tArgs[1])
y = tonumber(tArgs[2])
local slot = 1
while turtle.getItemCount(slot) == 0 do
local slot = slot + 1
if slot > 16 then
  local slot = 1
end
turtle.select(slot)
end
turtle.placeDown()
for j = 1, y do
  for i = 1, x-1 do
	 turtle.forward()
	 turtle.placeDown()
  end
  if j ~= y then
   if j % 2 == 0 then
	turtle.turnRight()
	turtle.forward()
	turtle.placeDown()
	turtle.turnRight()
   else
	turtle.turnLeft()
	turtle.forward()
	turtle.placeDown()
	turtle.turnLeft()
   end
  end
end
DaGamer12345 #6
Posted 17 August 2013 - 04:58 PM
You could do this:

local slot = 1 --Sets variable "slot" to 1
while turtle.getItemCount(slot) == 0 do --Executes following code if the current slot (and subsequent slots) are empty
local slot = slot + 1 --Raises variable "slot" by 1
if slot > 16 then --Executes following code if "slot" is 17+ (if this was not there, the program would error at one point)
  local slot = 1 --Resets "slot" to 1
end
turtle.select(slot) --Selects inventory slot specified by variable "slot"
end
Put this before the first turtle.placeDown()
this don't work at me. i just set it before the first turtle.placeDown() but it don't work.

local tArgs = {...}
turtle.forward()
print(tArgs[1]..":"..tArgs[2])
x = tonumber(tArgs[1])
y = tonumber(tArgs[2])
local slot = 1
while turtle.getItemCount(slot) == 0 do
local slot = slot + 1
if slot > 16 then
  local slot = 1
end
turtle.select(slot)
end
turtle.placeDown()
for j = 1, y do
  for i = 1, x-1 do
	 turtle.forward()
	 turtle.placeDown()
  end
  if j ~= y then
   if j % 2 == 0 then
	turtle.turnRight()
	turtle.forward()
	turtle.placeDown()
	turtle.turnRight()
   else
	turtle.turnLeft()
	turtle.forward()
	turtle.placeDown()
	turtle.turnLeft()
   end
  end
end
Okay, my bad. Code was not only over-complicated but non-functional. Use this instead:

for n=1,16 do --# For every slot
if turtle.getItemCount(n) > 0 then --# Check if there is anything in that particular slot
  turtle.select(n) --# If there is, select that slot
  break --# And then break out of the loop so we don't just select every slot that has things in it.
end
end
Edited by
thijs1444 #7
Posted 18 August 2013 - 02:44 AM
Okay, my bad. Code was not only over-complicated but non-functional. Use this instead:

for n=1,16 do --# For every slot
if turtle.getItemCount(n) > 0 then --# Check if there is anything in that particular slot
  turtle.select(n) --# If there is, select that slot
  break --# And then break out of the loop so we don't just select every slot that has things in it.
end
end
this still don't work because it don't check for the inv get empty. and if empty it select the next slot. now it just select at the begin or there is a slot with items.

for n=1,16 do --# For every slot
if turtle.getItemCount(n) > 0 then --# Check if there is anything in that particular slot
  turtle.select(n) --# If there is, select that slot
  break --# And then break out of the loop so we don't just select every slot that has things in it.  
  if turtle.getItemcount(n) = 0 then ----new part
  turtle.select(n)	---new part
  end
end
end
i thought something about this but this don't work. anyone have a idea how i can do it. or what is wrong at my part