Jump to content




attempt to call nil


  • You cannot reply to this topic
4 replies to this topic

#1 gheotic

  • Members
  • 64 posts
  • LocationDenmark

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 =)

#2 Flyin_Spaghetti

  • Members
  • 14 posts

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

#3 gheotic

  • Members
  • 64 posts
  • LocationDenmark

Posted 02 January 2013 - 04:57 AM

View PostFlyin_Spaghetti, on 02 January 2013 - 04:47 AM, said:

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

#4 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 02 January 2013 - 05:46 AM

View PostFlyin_Spaghetti, on 02 January 2013 - 04:47 AM, said:

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).

#5 ChunLing

  • Members
  • 2,027 posts

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users