Jump to content




Why am I getting a "expected number" error?


4 replies to this topic

#1 ShoeLace1291

  • Members
  • 8 posts

Posted 16 February 2020 - 02:42 AM

I am working on a turtle program that's supposed to build 4 walls. For some reason, I am getting the error "Expected number". The code from the line that the error is thrown from is code that I've used before(turtle.refuel(1) in other programs so I'm kind of confused right now. Forgive my ignorance on the subject, I have little experience with Lua. I'll point out in the code where the error is.


write("How wide should the room be?")
local x = tonumber(read())
write("How long should the room be?")
local y = tonumber(read())
write("How tall should the room be?")
local z = tonumber(read())

write("Place fuel in slot 1 and whatever building block you want to use in the rest and press enter to continue")

read()

function go()
    turtle.forward()
    slot = checkSlots()
    turtle.select(slot)
    turtle.placeUp()
    checkFuel()
end

function checkFuel()
    if turtle.getFuelLevel() == 0 then
        turtle.refuel(1) [b] --Error happens here[/b]
    end
end

function checkSlots()
    for s=2,16,1 do
        if turtle.getItemCount(s) > 0 then break end
    end
    return s
end

for h=0,z,1 do
  for i=0,1,1 do
    for w=0,y,1 do
        go()
    end
    turtle.turnRight()
    for l=0,x,1 do
        go()
    end
    turtle.turnRight()
  end
  turtle.digDown()
  turtle.down()
end


#2 Luca_S

  • Members
  • 407 posts
  • LocationGermany

Posted 16 February 2020 - 06:21 AM

Are you sure the Error is happening at turtle.refuel(1)? Because that should work, however your checkSlots function will always return nil, so turtle.select(slot) will break.

The problem is the scope of the variable s. It is only visible inside of the for loop, I would suggest changing your function to this:
function checkSlots()
	for s=2,16,1 do
		if turtle.getItemCount(s) > 0 then
		  return s
		end
	end
end


Also, after calling checkSlots() you should check wether the result is nil, in case there are no Blocks anymore.

Edited by Luca_S, 17 February 2020 - 04:02 PM.


#3 ShoeLace1291

  • Members
  • 8 posts

Posted 17 February 2020 - 10:50 PM

that did the trick. thanks very much!

#4 ShoeLace1291

  • Members
  • 8 posts

Posted 18 February 2020 - 01:00 AM

View PostLuca_S, on 16 February 2020 - 06:21 AM, said:

Also, after calling checkSlots() you should check wether the result is nil, in case there are no Blocks anymore.

Isn't that what turtle.getItemCount(s) does?

#5 Luca_S

  • Members
  • 407 posts
  • LocationGermany

Posted 18 February 2020 - 12:41 PM

View PostShoeLace1291, on 18 February 2020 - 01:00 AM, said:

View PostLuca_S, on 16 February 2020 - 06:21 AM, said:

Also, after calling checkSlots() you should check wether the result is nil, in case there are no Blocks anymore.

Isn't that what turtle.getItemCount(s) does?

If all slots from 2 to 16 are empty your function won't return anything, which will cause turtle.select(slot) to error.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users