The error occurs on line 112 when trying to use the goto() function.
I’m completely new to coding as of 4 days ago and am attempting to write a code that references a table for an argument to select a parameter for a function.
I want to enter changeD(a[4]) and have checkD() run until it returns a[4]. Instead i get the error code listed in the title.
My table is as follows
a = {}
a[1] = "north"
a[2] = "east"
a[3] = "south"
a[4] = "west"
I reference a table value as an argument in this function:
function changeD(arg)
local dir = 0
repeat
if checkD() == arg then
print("Done")
dir = 1
else
turn(1,1)
end
until dir == 1
end
the checkD() function returns one of the table values. Therefore the changeD() function runs until the value returned by checkD() is the same as the table value specified in the argument.
I apologize if i’m using the wrong words to refer to the table stuff as I am still wet behind the ears when it comes to coding.
My full program is as follows. Sorry if its messy.
c = 0
a = {}
a[1] = "north"
a[2] = "east"
a[3] = "south"
a[4] = "west"
function fd(x)
for i = 1 ,x do
print("forward")
turtle.dig()
turtle.forward()
end
end
function turn(y,x)
if x == 1 then
for i = 1 ,y do
turtle.turnLeft()
print("left")
end
elseif x == 2 then
for i = 1 ,y do
turtle.turnRight()
print("right")
end
end
end
function checkD()
local X, Y, Z = gps.locate()
print(X)
print(Y)
print(Z)
turtle.forward()
local x , y, z = gps.locate()
print(x)
print(y)
print(z)
c = 0
result = false
D = 0
repeat
if c == 0 then
A = X
B = x
print(A.." "..B)
else
A = Z
B = z
print(A.." "..B)
end
sleep(2)
if A < B then
g = 1
elseif A > B then
g = 2
elseif A == B then
c = 1
end
print("compared values; g="..g)
if c == 0 then
if g == 1 then
D = a[2]
result = true
elseif g == 2 then
D = a[4]
result = true
end
print("checked x")
print(D)
elseif not c == 0 then
if g == 1 then
D = a[3]
result = true
elseif g == 2 then
D = a[1]
result = true
end
print("checked z")
end
print("Direction facing "..D)
until result == true
turtle.back()
return D
end
function changeD(arg)
local dir = 0
repeat
if checkD() == arg then
print("Done")
dir = 1
else
turn(1,1)
end
until dir == 1
end
local x = 0
function goto(x,y,z)
local t = 0
local a = 0
local X, Y, Z = gps.locate()
print(X)
print(Y)
print(Z)
if X > x then
t = X - x
changeD(a[4])
fd(t)
elseif X < x then
t = x - X
changeD(a[2])
fd(t)
else
print("cord is same")
end
if Z > z then
t = Z - z
change(a[1])
fd(t)
elseif Z < z then
t = z - Z
change(a[3])
fd(t)
else
print("cord is same")
end
if Y > y then
t = Y - y
for i = 1,t do
turtle.down()
end
elseif Y < y then
t = y - Y
for i = 1,t do
turtle.up()
end
end
end
>Solution :
In goto, you declare local a = 0. This local a variable shadows the global a = {} variable. The local a variable is a number, so can’t be indexed.