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

attempt to call nil

Started by gheotic, 02 January 2013 - 03:42 AM
gheotic #1
Posted 02 January 2013 - 04:42 AM
Im trying to make a turtle that digs a tunnel system and im trying to use function i have never used that before so its pretty difficult

but every time i run my program it says
21:attempt to call nil


local fuel = 1
local x = 0
term.clear()
term.setCursorPos(1,1)
function getFuel()
if turtle.getFuelLevel() <=10 then
turtle.select(1)
turtle.refuel(2)   
end
function Detect()
while turtle.detect() do
turtle.dig()
end
end
end
while x < 16 do --digging the tunnel
Detect()
turtle.forward()
turtle.digDown()
turtle.turnRight()
Detect()
turtle.forward()
turtle.turnLeft()
turtle.digDown()
Detect()
turlle.forward()
turtle.digDown()
turtle.turnLeft()
Detect()
turtle.forward()
turtle.turnRight()
turtle.digDown()
x = x + 1
end

any help would be appreciated =)
Flyin_Spaghetti #2
Posted 02 January 2013 - 04:47 AM
you defined function Detect() inside the function getFuel(). therefore it is practically unusable unless you call it inside getFuel, which in line 21, you do not
gheotic #3
Posted 02 January 2013 - 04:57 AM
you defined function Detect() inside the function getFuel(). therefore it is practically unusable unless you call it inside getFuel, which in line 21, you do not

ohh thanks a lot
Orwell #4
Posted 02 January 2013 - 05:46 AM
you defined function Detect() inside the function getFuel(). therefore it is practically unusable unless you call it inside getFuel, which in line 21, you do not
It's still accessible out of the getFuel function. But only after calling getFuel a first time. If he would've called Detect after getFuel, it wouldn't have crashed (it's still bad coding practice though).
ChunLing #5
Posted 02 January 2013 - 04:07 PM
Defining things globally just to avoid scope issues is bad practice, defining them globally from inside of a function you don't call is catastrophic practice resulting from habitual bad practice.