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

edit tes

Started by IndustrialLemon, 15 March 2014 - 03:32 PM
IndustrialLemon #1
Posted 15 March 2014 - 04:32 PM
So heres the deal. I ripped this code straight from the wiki page on Open CC Sensors, so I could start learning how to use it for player tracking, and it gives me the the error message "test :10: attempt to index ? (a nil value)".
I have no idea what to do!?
Can anyone help me?

os.loadAPI("ocs/apis/sensor")

-- the location of the redstone lamp relative to the sensor
local offset = {
  X = 1,
  Y = 1,
  Z = 0
}

-- how close a player has to be to activate the lamp
local radius = 5
-- find the distance from the player position to the offset
function distance(pos)
  local xd = pos.X - offset.X
  local yd = pos.Y - offset.Y
  local zd = pos.Z - offset.Z
  return math.sqrt(xd*xd + yd*yd + zd*zd)
end

local proximity = sensor.wrap("left")
while true do
  local signal = false
  local targets = proximity.getTargets()
  for k, v in pairs(targets) do
		if distance(v.Position) < radius then
		  signal = true  
		end
  end
  rs.setOutput("top", signal)
end
CometWolf #2
Posted 15 March 2014 - 04:35 PM
Line 10 is

-- how close a player has to be to activate the lamp
Which is a comment, so this is obviously not your whole code.
IndustrialLemon #3
Posted 15 March 2014 - 04:48 PM
Oh sorry. Well I took out the comments when I ran it. Line ten is really
local xd = pos.X - offset.X
CometWolf #4
Posted 15 March 2014 - 05:05 PM
The only line that calls the distance function is this

			    if distance(v.Position) < radius then
Meaning that v.Position is nil, thus causing the error.
v is a value in the table targets

  for k, v in pairs(targets) do
and finally targets is the result of

  local targets = proximity.getTargets()
Meaning that your sensor isn't detecting any targets. Does it have a card installed?
IndustrialLemon #5
Posted 15 March 2014 - 07:39 PM
It does. I actually am using a turtle. The card is in the 16th slot so unless I'm mistaken that's the slot you keep the sensor card.
CometWolf #6
Posted 15 March 2014 - 08:25 PM
Which card?
IndustrialLemon #7
Posted 15 March 2014 - 08:41 PM
Proximity Sensor Card Mk. 4
CometWolf #8
Posted 15 March 2014 - 08:52 PM
See if you can get the included sensorview program to work
IndustrialLemon #9
Posted 15 March 2014 - 08:59 PM
If you were meaning the other included program on the wiki page then I ran that and I got no issues,
CometWolf #10
Posted 15 March 2014 - 09:02 PM
No i meant the included program, the one already installed on your computer in the ocs folder. However, since you got another sensor related program to work at all, and you apparently took out all the comments, im guessing you wrote the program in by hand? If so, please upload it to pastebin, or take a closer look at it. Chances are you did something wrong.
ElvishJerricco #11
Posted 15 March 2014 - 09:04 PM
Meaning that your sensor isn't detecting any targets.

Nope. If this were true the loop simply wouldn't ever run. The problem is elsewhere. Probably a misuse of the API, meaning it's evolved since the wiki-page was last updated.
IndustrialLemon #12
Posted 15 March 2014 - 09:05 PM
Thats what I had figured. if you could maybe find a way to check the coords of a nearby player for me that'd be fantastic.
IndustrialLemon #13
Posted 15 March 2014 - 09:29 PM
Oh goodie! Got it working everybody. I completely rewrote the program how it was on the forums but never did find what was wrong with mine. It works now though :)/>
CometWolf #14
Posted 15 March 2014 - 09:30 PM
Next time use pastebin to transfer code directly instead of writing it by hand.

Probably a misuse of the API, meaning it's evolved since the wiki-page was last updated.
This is incorrect, the API has not changed.
Anavrins #15
Posted 16 March 2014 - 02:44 AM
Ehh, "Sensors cards" are suposed to go in the "Sensor" block, which then act as a normal peripheral.
CometWolf #16
Posted 16 March 2014 - 09:58 AM
Not if the sensor is attached to the turtle, like a a modem or a pickaxe.
Dog #17
Posted 16 March 2014 - 10:02 PM
In OpenCCSensors the turtle has to place the particular sensor 'card' in slot 16 and mount it as "left" if I remember correctly
Lyqyd #18
Posted 16 March 2014 - 10:07 PM
The turtle must be a sensor turtle (craft a sensor with a turtle to create) and then just have the card in slot sixteen. No need to "mount" anything. :)/>