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

attempt to compare string with number expected, got string

Started by floguaz, 24 May 2013 - 02:11 PM
floguaz #1
Posted 24 May 2013 - 04:11 PM
Hi, I'm begining in lua and I made a program to automaticaly have obsidian, and put it in a chest.
Here's the code :


print("How many obsidian ?")
local nombreDemande = read()	-- read the number of obsidian requested
local nombreMine = 0	-- number of mined obsidian
term.clear()
term.setCursorPos(1,1)
print("Commande : "..nombreDemande)

while nombreMine <= nombreDemande do

if turtle.getFuelLevel() < 10 then
  turtle.select(1)
  turtle.refuel(1)
end
  turtle.select(2)	-- Placing redstone
  turtle.forward()
  turtle.forward()
  turtle.place()
  turtle.back()
  turtle.place()
  turtle.back()
  turtle.place()

  rs.setOutput("left", true)	-- Desactivate piston and lava goes on redstone
  sleep(8)  
  rs.setOutput("left", false)	-- Reactivate piston
  sleep(2)

  if turtle.getFuelLevel() < 10 then
	turtle.select(1)
	turtle.refuel(1)
  end

  turtle.dig()  -- Mine the obsidian
  nombreMine = nombreMine + 1
  turtle.forward()
  turtle.dig()
  nombreMine = nombreMine + 1
  turtle.forward()
  turtle.dig()
  nombreMine = nombreMine + 1

  turtle.back()	-- Return on the default place
  turtle.back()
  turtle.select(3)	-- Put the obsidian in the chest
  turtle.turnRight()
  turtle.drop()
  turtle.turnLeft()
end

print("Here are your obsidians")

If it don't works, here's pastebin : http://pastebin.com/7QKCeCtr

Edit : I forgot it. When I put the number of obsidian I'm looking for, the error appears :
untitled:9: attempt to compare string with number expected, got string


Sorry for my english, I'm french
Thanks for your help
floguaz
Edited on 24 May 2013 - 03:42 PM
Lyqyd #2
Posted 24 May 2013 - 05:41 PM
Split into new topic.

Use tonumber(read()).
floguaz #3
Posted 25 May 2013 - 01:43 AM
And for the name of the variable ? What does "tonumber" ?

Thanks
floguaz
superaxander #4
Posted 25 May 2013 - 01:45 AM
tonumber translates the string you get from read() to a number so you could put it inside a variable like so
variable = tonumber(read())

Edit: yay 500th post
floguaz #5
Posted 25 May 2013 - 01:52 AM
Ok thanks ! It works !
At the begining of my program, it has worked, so why now it does that ?