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

startup:64: Attempt To Perform Arithmetic __div on nil and number

Started by roban100, 12 December 2012 - 06:48 AM
roban100 #1
Posted 12 December 2012 - 07:48 AM
Hello!

So yesterday and today i have tried to make a turtle dig kinda advanced(for me)v1.0 i call it anyway i have some problems

(i enter the rednet information from a manager computer i have it simply sends !dig("what the user types", "what the user types") and sends it to the turtle:)

1. For example: I input !dig(3,3) to it it goes to position and digs down 1 and goes on put stops at two and in the end it makes a hole that is 3deap 3 long and only 2 wide why?

2. It can't come up gives me this error: startup:64: Attempt To Perform Arithmetic __div on nil and number

Please help i have thought about this very long and i guess it's some easy explanation that i have missed somewhere.

Here is the code for the turtle (feel free to try and use it and figure out a solution :)/> !)
rednet.open("right")
local y=0
local manageServerId = 12
local errors=0
term.clear()
term.setCursorPos(1,1)
print("Mining Turtle 1.0 Initialized!")
function goUp(loopAmount)
for i=1, loopAmount do
if turtle.up() then
y=y-1
return true
else
errors=errors+1
return false
end
end
end
function forward()
if turtle.forward() then
return true
else
errors=errors+1
return false
end
end
function forwardLoop(loopAmount)
for i=1, loopAmount do
if turtle.forward() then
return true
else
errors=errors+1
return false
end
end
end
function digForward()
if turtle.dig() and turtle.forward() then
return true
else
errors=errors+1
return false
end
end
function digForwardLoop(loopAmount)
for i=1, loopAmount do
if turtle.dig() and turtle.forward() then
return true
else
errors=errors+1
return false
end
end
end
function returnToSurface()
forwardLoop(math.floor(radius/2))
turtle.turnLeft()
forward()
if math.fmod(y, 2) == 0 then
turtle.turnLeft()
turtle.turnLeft()
end
goUp(y)
print("And Were Up! \nFuel Level Is At: "..turtle.getFuelLevel())
rednet.send(managerServerId, "And Were Up! \nFuel Level Is At: "..turtle.getFuelLevel())
end
function dig(deapth, radius)
turtle.refuel(1)
rednet.send(manageServerId, "Starting To Dig!")
for i=1, math.floor(radius/2) do
if turtle.back() then
else
errors=errors+1
end
end
turtle.turnRight()
for i=1, math.floor(radius/2) do
if turtle.back() then
else
errors=errors+1
end
end
rednet.send(manageServerId, "We have moved in position!")
while errors <= 7 do
if y<=deapth then
if turtle.digDown() and turtle.down() then
y=y+1
else
errors=errors+1
end
for i=1, radius do
if i ~= radius then
if math.fmod(i, 2)==0 then
digForwardLoop(radius)
turtle.turnRight()
digForward()
turtle.turnRight()
else
digForwardLoop(radius)
turtle.turnLeft()
digForward()
turtle.turnLeft()
end
else
if math.fmod(i, 2)==0 then
digForwardLoop(radius)
turtle.turnLeft()
digForwardLoop(radius)
turtle.turnLeft()
else
digForwardLoop(radius)
turtle.turnLeft()
turtle.turnLeft()
end
end
end
elseif y > deapth then
print("Coming Up!")
rednet.send(managerServerId, "Coming Up!")
print(errors)
break
end
end
returnToSurface()
end

while true do
senderId, message, distance = rednet.receive()

local b = string.sub(string.gsub(message, "%)", ""),5)
local c = string.gsub(b, "%(", "")
local d = string.gsub(c, ",.*", "")
local r = string.gsub(b, "%(.*,", "")
print("Using Deapth: "..d.. " And Radius: "..r)
dig(tonumber(d-1), tonumber(r))
end
OmegaVest #2
Posted 12 December 2012 - 10:37 AM
Where do you initialize radius? Remembering that any variables initialized as a function parameter automatically calls as a local variable, and will not pass it to another function unless you put it in that function's parameters.

So:


function upper(someP, someA)
   lopside()
end
lopside()
   someP = someA
end

Does not work. And I think that may have been what you did here.


Also, format your code. Especially for something this long, because it helps us help you.
roban100 #3
Posted 13 December 2012 - 03:21 AM
Where do you initialize radius? Remembering that any variables initialized as a function parameter automatically calls as a local variable, and will not pass it to another function unless you put it in that function's parameters.

So:


function upper(someP, someA)
   lopside()
end
lopside()
   someP = someA
end

Does not work. And I think that may have been what you did here.


Also, format your code. Especially for something this long, because it helps us help you.

Thanks very much! i managed to solve it and some other problems(pretty easy ones that i've missed).

so here is the code now it only has one ("problem") when it comes up it sais its many errors and if u send it far down it gets more than 7 errors and stops why (sometines nothing)?

 1: rednet.open("right")
  2: local radius
  3: local deapth
  4: local y=0
  5: local manageServerId = 12
  6:
  7: local errors=0
  8:
  9: term.clear()
10: term.setCursorPos(1,1)
11: print("Mining Turtle 1.0 Initialized!")
12:
13: function goUp(loopAmount)
14:  for i=1, loopAmount do
15:   if turtle.up() then
16:	y=y-1
17:   else
18:	errors=errors+1
19:   end
20:  end
21: end
22:
23: function forward()
24:  if turtle.forward() then
25:   return true
26:  else
27:   errors=errors+1
28:   return false
29:  end
30: end
31:
32: function forwardLoop(loopAmount)
33:  for i=1, loopAmount do
34:   if turtle.forward() then
35:  
36:   else
37:	errors=errors+1
38:   end
39:  end
40: end
41:
42: function digForward()
43:  if turtle.dig() and turtle.forward() then
44:  
45:  else
46:   errors=errors+1
47:  end
48: end
49:
50: function digForwardLoop(loopAmount)
51:  for i=2, loopAmount do
52:   if turtle.dig() and turtle.forward() then
53:  
54:   else
55:	errors=errors+1
56:   end
57:  end
58: end
59:
60: function returnToSurface()
61:
62: forwardLoop(math.floor(radius/2))
63:
64:  if math.fmod(radius, 2) == 0 then
65:   turtle.turnRight()
66:  else
67:   turtle.turnLeft()
68:  end
69:
70: forwardLoop(math.floor(radius/2))
71:  if math.fmod(y, 2) ~= 0 then
72:   turtle.turnLeft()
73:   turtle.turnLeft()
74:  end
75:
76: goUp(y)
77:
78:  if turtle.detect()==false then
79:   forward()
80:  end
81:
82: print("\nAnd Were Up!\nFuel Level Is At: "..turtle.getFuelLevel().."\nErrors Under Trip: "..errors)
83: rednet.send(managerServerId, "And Were Up! \nFuel Level Is At: "..turtle.getFuelLevel().."\nErrors Under Trip: "..errors)
84: tempY=0
85: y=0
86: errors=0
87: end
88:
89: function dig(d, r)
90: radius = r
91: deapth = d
92: turtle.refuel(1)
93:
94: rednet.send(manageServerId, "Starting To Dig!")
95:
96:  for i=1, math.floor(radius/2) do
97:   if turtle.back() then
98:  else
99:   errors=errors+1
100:  end
101: end
102:
103: turtle.turnRight()
104:
105: for i=1, math.floor(radius/2) do
106:  if turtle.back() then
107:  
108:  else
109:   errors=errors+1
110:  end
111: end
112:
113: rednet.send(manageServerId, "We have moved in position!")
114:
115: while errors <= 7 do
116:  if y<=deapth then
117:   if turtle.digDown() and turtle.down() then
118:	y=y+1
119:   else
120:	errors=errors+1
121:  end
122:
123: for i=1, radius do
124:  if i ~= radius then
125:   if math.fmod(i, 2)==0 then
126:	digForwardLoop(radius)
127:
128:	turtle.turnRight()
129:
130:	digForward()
131:
132:	turtle.turnRight()
133:   else
134:	digForwardLoop(radius)
135:
136:	turtle.turnLeft()
137:
138:	digForward()
139:
140:	turtle.turnLeft()
141:
142:   end
143:  else
144:
145:   if math.fmod(i, 2)==0 then
146:	digForwardLoop(radius)
147:
148:	turtle.turnLeft()
149:	digForwardLoop(radius)
150:	turtle.turnLeft()
151:  
152:   else
153:  
154:	digForwardLoop(radius)
155:
156:	turtle.turnLeft()
157:	turtle.turnLeft()
158:
159:   end
160:  end
161: end
162:
163:  elseif y > deapth then
164:   print("Coming Up!")
165:   rednet.send(managerServerId, "Coming Up!")
166:   break
167:  end
168:
169:  if turtle.getFuelLevel() < 5 then
170:   turtle.refuel(7)
171:  end
172: end
173: returnToSurface()
174: end
175:
176:
177: while true do
178:
179: senderId, message, distance = rednet.receive()
180:
181:
182: local b = string.sub(string.gsub(message, "%)", ""),5)
183: local c = string.gsub(b, "%(", "")
184: local d = string.gsub(c, ",.*", "")
185: local r = string.gsub(b, "%(.*,", "")
186:
187: print("Using Deapth: "..d.. " And Radius: "..r)
188:
189: dig(tonumber(d-1), tonumber(r))
190:
191: end
PixelToast #4
Posted 13 December 2012 - 03:24 AM
can you give us the errors, and code without the numbers
roban100 #5
Posted 13 December 2012 - 03:29 AM
can you give us the errors, and code without the numbers

No you missunderstand the errors that i have in a local variable that becomes more if it fails!
roban100 #6
Posted 13 December 2012 - 03:52 AM
Problems solved! thanks!
(yeah i formed those functions as guns :lol:/> )
code:

  1: rednet.open("right")
  2: local radius
  3: local deapth
  4: local y=0
  5: local manageServerId = 12
  6:
  7: local errors=0
  8:
  9: term.clear()
10: term.setCursorPos(1,1)
11: print("Mining Turtle 1.0 Initialized!")
12:
13: function goUp(loopAmount)
14:  for i=1, loopAmount do
15:   if turtle.up() then
16:	y=y-1
17:   else
18:	errors=errors+1
19:   end
20:  end
21: end
22:
23: function forward()
24:  if turtle.forward() then
25:   return true
26:  else
27:   errors=errors+1
28:   return false
29:  end
30: end
31:
32: function forwardLoop(loopAmount)
33:  for i=1, loopAmount do
34:   if turtle.forward() then
35:  
36:   else
37:	errors=errors+1
38:   end
39:  end
40: end
41:
42: function digForward()
43:  if turtle.dig() and turtle.forward() then
44:  
45:  else
46:   errors=errors+1
47:  end
48: end
49:
50: function digForwardLoop(loopAmount)
51:  for i=2, loopAmount do
52:   if turtle.dig() and turtle.forward() then
53:  
54:   else
55:	errors=errors+1
56:   end
57:  end
58: end
59:
60: function returnToSurface()
61:
62: forwardLoop(math.floor(radius/2))
63:
64:  if math.fmod(radius, 2) == 0 then
65:   turtle.turnRight()
66:  else
67:   turtle.turnLeft()
68:  end
69:
70: forwardLoop(math.floor(radius/2))
71:  if math.fmod(y, 2) ~= 0 then
72:   turtle.turnLeft()
73:   turtle.turnLeft()
74:  end
75:
76: goUp(y)
77:
78:  if turtle.detect()==false then
79:   forward()
80:  end
81:
82: print("\nAnd Were Up!\nFuel Level Is At: "..turtle.getFuelLevel().."\nErrors Under Trip: "..errors)
83: rednet.send(managerServerId, "And Were Up! \nFuel Level Is At: "..turtle.getFuelLevel().."\nErrors Under Trip: "..errors)
84: tempY=0
85: y=0
86: errors=0
87: end
88:
89: function dig(d, r)
90: radius = r
91: deapth = d
92: turtle.refuel(1)
93:
94: rednet.send(manageServerId, "Starting To Dig!")
95:
96:  for i=1, math.floor(radius/2) do
97:   if turtle.back() then
98:  else
99:   errors=errors+1
100:  end
101: end
102:
103: turtle.turnRight()
104:
105: for i=1, math.floor(radius/2) do
106:  if turtle.back() then
107:  
108:  else
109:   errors=errors+1
110:  end
111: end
112:
113: rednet.send(manageServerId, "We have moved in position!")
114:
115: while errors <= 7 do
116:  if y<=deapth then
117:   if turtle.digDown() and turtle.down() then
118:	y=y+1
119:   else
120:	errors=errors+1
121:  end
122:
123: for i=1, radius do
124:  if math.fmod(y, 2) == 0 then
125:	if i ~= radius then
126:	if math.fmod(i, 2)==0 then
127:	 digForwardLoop(radius)
128:
129:	 turtle.turnLeft()
130:
131:	 digForward()
132:
133:	 turtle.turnLeft()
134:	 turtle.turnLeft()
135:	else
136:	 digForwardLoop(radius)
137:
138:	 turtle.turnRight()
139:
140:	 digForward()
141:
142:	 turtle.turnRight()
143:
144:	end
145:   else
146:
147:	if math.fmod(i, 2)==0 then
148:	 digForwardLoop(radius)
149:
150:	 turtle.turnLeft()
151:	 turtle.turnLeft()
152:	
153:	else
154:  
155:	 digForwardLoop(radius)
156:
157:	 turtle.turnLeft()
158:	 turtle.turnLeft()
159:
160:	end
161:   end
162:  else
163:   if i ~= radius then
164:	if math.fmod(i, 2)==0 then
165:	 digForwardLoop(radius)
166:
167:	 turtle.turnRight()
168:
169:	 digForward()
170:
171:	 turtle.turnRight()
172:	else
173:	 digForwardLoop(radius)
174:
175:	 turtle.turnLeft()
176:
177:	 digForward()
178:
179:	 turtle.turnLeft()
180:
181:	end
182:   else
183:
184:	if math.fmod(i, 2)==0 then
185:	 digForwardLoop(radius)
186:
187:	 turtle.turnLeft()
188:	 digForwardLoop(radius)
189:	 turtle.turnLeft()
190:	
191:	else
192:  
193:	 digForwardLoop(radius)
194:
195:	 turtle.turnLeft()
196:	 turtle.turnLeft()
197:
198:	end
199:   end
200:  end
201: end
202:
203:  elseif y > deapth then
204:   print("Coming Up!")
205:   rednet.send(managerServerId, "Coming Up!")
206:   break
207:  end
208:
209:  if turtle.getFuelLevel() < 5 then
210:   turtle.refuel(7)
211:  end
212: end
213: returnToSurface()
214: end
215:
216:
217: while true do
218:
219: senderId, message, distance = rednet.receive()
220:
221:
222: local b = string.sub(string.gsub(message, "%)", ""),5)
223: local c = string.gsub(b, "%(", "")
224: local d = string.gsub(c, ",.*", "")
225: local r = string.gsub(b, "%(.*,", "")
226:
227: print("Using Deapth: "..d.. " And Radius: "..r)
228:
229: dig(tonumber(d-1), tonumber(r))
230:
231: end