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

How can I get this to work?

Started by codedHamster, 15 September 2012 - 08:25 AM
codedHamster #1
Posted 15 September 2012 - 10:25 AM
I keep getting this error:

test:17: attempt to perform arithmetic __sub on nil and number

Here's the code:

local tArgs = { ... }
if #tArgs == 3 then
	local x,y,z
	x = tonumber(tArgs[1])
	y = tonumber(tArgs[2])
	z = tonumber(tArgs[3])
	print( ""..x..","..y..","..z )
	os.sleep(1)
else
	print( "blah" )
	return
end

rednet.open("right")
local cx,cy,cz = gps.locate(2, true)
print( ""..cx..","..cy..","..cz )
x = x-cx
y = y-cy
z = z-cz
print( ""..x..","..y..","..z )
Hepcat2299 #2
Posted 15 September 2012 - 10:56 AM
Im no expert and just stumbled through your code like a novice but I think I found the problem. For whatever reason your program is forgetting to user inputted variables at the start. My solution was to do something like this:


local tArgs = { ... }
if #tArgs == 3 then
		local x,y,z
		x = tonumber(tArgs[1])
		y = tonumber(tArgs[2])
		z = tonumber(tArgs[3])
		print( ""..x..","..y..","..z )
		os.sleep(1)
else
		print( "blah" )
		return
end
rednet.open("right")
local cx,cy,cz = gps.locate(3, true)
print(cx..","..cy..","..cz )
	 local x,y,z
		x = tonumber(tArgs[1])
		y = tonumber(tArgs[2])	-- Notice I recall the tArgs
		z = tonumber(tArgs[3])

x = x-cx
y = y-cy
z = z-cz
print(x..","..y..","..z )
Lyqyd #3
Posted 15 September 2012 - 06:21 PM
You don't need to have that much duplicate code as in the post above, you should just need to move the local x,y,z line above the if statement just above it.