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

[Lua][Error] if turtle.detect then

Started by IceCrasher, 07 October 2012 - 08:38 PM
IceCrasher #1
Posted 07 October 2012 - 10:38 PM
hi guys, i get ever and ever the same error please help :D/>/>

bios:328: [string "skyside"]:30: '(' expected

here is the programm:


local fuellevel = 0
local cx, cy, cz = 0, 0, 0
local nx, ny, nz = 0, 0, 0
direct = 0
rednet.open("right")
cx, cy, cz = gps.locate(1)
if x and y and z then
   print(x..y..z)
else
  print("Couldn't get position")
end
function doit()
fuellevel = turtle.getFuelLevel()
if fuellevel < 0 then
   print("Please put fuel in slot1.")
end
while fuellevel < 0 do
   turtle.refuel()
end
print("Thank you.")
function finddirect1
	   if turtle.detect() then
	   print("I need space in front of me!")
	   while turtle.detect do
			    os.sleep(1)
		  return
	   end
	   return
    else
	   finddirect2
    end
end
function finddirect2
   nx, ny, nz = gps.locate(1)
   if nx < ox then
	  direct = 1
   end
   if nx >  ox then
	  direct = 3
   end
   if nz < oz then
	  direct = 4
   end
   if nz > oz then
	  direct = 2
   end
end  
finddirect1()

thanks
Lyqyd #2
Posted 07 October 2012 - 10:43 PM
Have you tried adding '()' to 'finddirect2' on line thirty, making it 'finddirect2()'?
Orwell #3
Posted 07 October 2012 - 10:44 PM
Have you tried adding '()' to 'finddirect2' on line thirty, making it 'finddirect2()'?

Same goes for finddirect1 (just saying to be sure :D/>/> )
jag #4
Posted 07 October 2012 - 11:09 PM
And you forgot the () on line 30, at turtle.detect
Pharap #5
Posted 08 October 2012 - 03:12 AM
And you forgot the () on line 30, at turtle.detect

If you're thinking that's line 30, you must be leaving at least about 6 lines of blank space.
Regardless, your point is valid.

hi guys, i get ever and ever the same error please help :D/>/>

bios:328: [string "skyside"]:30: '(' expected

thanks

firstly, error message:

bios:328: <– indicates one issue in bios
[string "skyside"] <– indicates more data about that error
:30: <– indicates line in your code causing the error
'(' expected <– tells you the problem

secondly, reworked code, which should no longer error:


local fuellevel = 0
local cx, cy, cz = 0, 0, 0
local nx, ny, nz = 0, 0, 0
direct = 0 -- this might need to be local if it's not used outside the program
--try to declare functions before using them
function finddirect2()
   nx, ny, nz = gps.locate(1) --I think gps needs more args than this, but I could be wrong, it's been ages since I used GPS
   if nx < ox then
		  direct = 1
   elseif nx >  ox then -- else ifs save time and end statements
		  direct = 3
   elseif nz < oz then
		  direct = 4
   elseif nz > oz then
		  direct = 2
   end
end 

function finddirect1
		   if turtle.detect() then
		   print("I need space in front of me!")
		   while turtle.detect do
						    os.sleep(1)
				  return
		   end
		   return
    else
		   finddirect2()
    end
end
function doit()
fuellevel = turtle.getFuelLevel()
if fuellevel < 0 then
   print("Please put fuel in slot1.")
end
while fuellevel < 0 do
   turtle.refuel()
end
print("Thank you.")
end -- always end functions
--the main stuff:
rednet.open("right")
cx, cy, cz = gps.locate(1)
if x and y and z then
   print(x..y..z)
else
  print("Couldn't get position")
end
finddirect1()

If there are any more issues, the forums are here
IceCrasher #6
Posted 08 October 2012 - 10:16 AM
so ok, thank you all and yay I'm a noob :D/>/>

so I fixed the programm but there is one little thingy that don't want to work ;)/>/>

when I run the programm it says:
772, 4, -284
skyside:48: attempt to compare nil with number

I really don't know why, because in line 8 it works :/




local fuellevel = 0
local cx, cy, cz = nil, nil, nil
local nx, ny, nz = nil, nil, nil
-- local  x,  y,  z = nil, nil, nil
direct = 0

rednet.open("right")
cx, cy, cz = gps.locate(3)

if cx and cy and cz then
   print(cx..", "..cy..", "..cz)
   doit()
   finddirect1()
   print(""..direct) 
else
  print("Couldn't get position")
end

function  doit()
   fuellevel = turtle.getFuelLevel()
   if fuellevel < 0 then
      print("Please put fuel in slot1.")
      while fuellevel < 0 do
         turtle.refuel()
         fuellevel = turtle.getFuelLevel()
         os.sleep(1)
         return
      end
      print("Thank you :D/>/>")
    end
end

function finddirect1()
   if turtle.detect() then
       print("I need space in front of me!")
       while turtle.detect do 
                os.sleep(1)
          return
       end
       return
    else
        turtle.forward()
        finddirect2() 
    end
end

function finddirect2()
   nx, ny, nz = gps.locate(3)
   if nx and ny and nz then
      if nx < ox then
         direct = 1
      end
      if nx >  ox then
         direct = 3
      end
      if nz < oz then
         direct = 4
      end
      if nz > oz then
         direct = 2
      end
   end
end
KaoS #7
Posted 08 October 2012 - 10:38 AM
if nx<ox then

where are you getting ox from? it is nil because it has not been assigned a value, you cannot see if nil is bigger than a number
Pharap #8
Posted 08 October 2012 - 10:47 AM
so ok, thank you all and yay I'm a noob :D/>/>

so I fixed the programm but there is one little thingy that don't want to work ;)/>/>

when I run the programm it says:
772, 4, -284
skyside:48: attempt to compare nil with number

I really don't know why, because in line 8 it works :/




local fuellevel = 0
local cx, cy, cz = nil, nil, nil
local nx, ny, nz = nil, nil, nil
-- local  x,  y,  z = nil, nil, nil
direct = 0

rednet.open("right")
cx, cy, cz = gps.locate(3)

if cx and cy and cz then
   print(cx..", "..cy..", "..cz)
   doit()
   finddirect1()
   print(""..direct)
else
  print("Couldn't get position")
end

function  doit()
   fuellevel = turtle.getFuelLevel()
   if fuellevel < 0 then
	  print("Please put fuel in slot1.")
	  while fuellevel < 0 do
		 turtle.refuel()
		 fuellevel = turtle.getFuelLevel()
		 os.sleep(1)
		 return
	  end
	  print("Thank you :D/>/>")
	end
end

function finddirect1()
   if turtle.detect() then
	   print("I need space in front of me!")
	   while turtle.detect do
				os.sleep(1)
		  return
	   end
	   return
	else
		turtle.forward()
		finddirect2()
	end
end

function finddirect2()
   nx, ny, nz = gps.locate(3)
   if nx and ny and nz then
	  if nx < ox then
		 direct = 1
	  end
	  if nx >  ox then
		 direct = 3
	  end
	  if nz < oz then
		 direct = 4
	  end
	  if nz > oz then
		 direct = 2
	  end
   end
end

You are trying to compare nx and nz with the non-existant ox and oz. Basically you appear to be using variables that haven't been assigned.
That and it's possible gps can't do the location and is returning nil. You might want to test if values are nil after the gps.locate() and then if they are, get the code to try again a few times before aborting the script.
IceCrasher #9
Posted 08 October 2012 - 11:09 AM
thank you guys, know my first usefull programm is working xD
Pharap #10
Posted 08 October 2012 - 11:18 AM
Glad to be of help. If you need any more help, be sure to ask the forums, we have nothing better to do.
KaoS #11
Posted 08 October 2012 - 11:33 AM
Glad to be of help. If you need any more help, be sure to ask the forums, we have nothing better to do.

lol speak for yourself :D/>/> I'm at work