Posted 25 December 2015 - 09:31 PM
Hello this is my first post so I'm sorry if I don't have the proper format down but I digress. Today I finally managed to get my player radar program running but to my dismay it seems to be a very unreliable bit of code which fails inexplicably. My ultimate goal is to make an integrated system between the sensor and the terminal glasses so that I can see outlines of players in a sort of HUD (through blocks ect) as well as see there relative position to me on a mini map. For the moment I'm just building this block of code which will tell me a players coords.
When the code does error its always with the 17th line of code. The error I get is "bad argument; table expected, got nil" I'm not sure if its an error in my code though, as it only happens occasionally and the other times it works as one would expect it to. I haven't been able to tell what causes it. I know that it doesn't happen if i stand still, that it does happen when I move but also not all the time I move and that if it errors out and I stop moving and am in the place it errored out in, it will continue to error out for a few seconds (the time it takes to recover to normal varies) and then it will be fine again until I move.
This truly baffles me since its such an erratic error, any help that can be offered would be very much appreciated, thank you.
A_Tasty_Peanut
os.loadAPI("ocs/apis/sensor")
local s = sensor.wrap("back")
local POS = {}
local names = {}
local mon = peripheral.wrap("top")
mon.setBackgroundColor(8)
mon.setTextScale(0.5)
local function scan(peris)
POS = {}
names = {}
local r = 0
local rawTargetData = peris.getTargets()
if rawTargetData then
for k,v in pairs(rawTargetData) do
if v["IsPlayer"]==true then
for z,x in pairs(peris.getTargetDetails(k)) do --this line errors
if z=="Position" then
r = r + 1
names[r] = k
POS[r] = x
end
end
end
end
end
end
while true do
scan(s)
mon.clear()
for i=1,#names do
mon.setCursorPos(1,1+(i-1)*4)
mon.setTextColor(16)
mon.write(names[i])
mon.setCursorPos(1,2+(i-1)*4)
mon.setTextColor(128)
mon.write("X: "..tostring((POS[i]["X"]-562))) --561 is the sensors x coord in the world
mon.setCursorPos(1,3+(i-1)*4)
mon.write("Y: "..tostring((POS[i]["Y"]+67))) --67 is its Y coord
mon.setCursorPos(1,4+(i-1)*4)
mon.write("Z: "..tostring((POS[i]["Z"]-1142))) --1141 is its Z coord
end
sleep(.1)
end
When the code does error its always with the 17th line of code. The error I get is "bad argument; table expected, got nil" I'm not sure if its an error in my code though, as it only happens occasionally and the other times it works as one would expect it to. I haven't been able to tell what causes it. I know that it doesn't happen if i stand still, that it does happen when I move but also not all the time I move and that if it errors out and I stop moving and am in the place it errored out in, it will continue to error out for a few seconds (the time it takes to recover to normal varies) and then it will be fine again until I move.
This truly baffles me since its such an erratic error, any help that can be offered would be very much appreciated, thank you.
A_Tasty_Peanut