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

OPA sensor proximity door nil value errors

Started by niels2398, 16 April 2015 - 10:47 AM
niels2398 #1
Posted 16 April 2015 - 12:47 PM
So I took Tamtam18_2's player detector and edited it slightly to fit my needs.

I changed the range and added an if statement to verify that its me who enters the door.
my version : http://pastebin.com/552RKCZB

after a bit it randomly brakes and gives me an attempt to index nil value error at line 19 or 40
not sure if it gets caused by going out of the detection range or something else. any idees?
flaghacker #2
Posted 16 April 2015 - 03:59 PM
Those errors are caused by there being no players in range.

p.getPlayers () would then return nil, and threating it as a table causes errors. The solution would be to check if the method returned nil.


players = p.getPlayers ()

if players == nil then
  print ("No players in range") --handle appropriately
else
  print (players [1].name)
end

You have to chech this every time you want to use getPlayers () , or you could check it once and call your functions if it's not nil.
Edited on 16 April 2015 - 02:00 PM
niels2398 #3
Posted 16 April 2015 - 04:41 PM
befor i do getUsername() I do :

if #p.getPlayers() ~= 0 then

is this wrong should i chance it to

if p.getPlayers() == nil then
print ("No players in range")
else
rest of the chkPlayer function here
end
flaghacker #4
Posted 16 April 2015 - 07:42 PM
You're right. You can't use #players, because # only works on tables, and nil is not a table.

You should also keep the players in a variable, like I did in my example, to ensure you're working on the same value. The return of getPlayers () could change between invocations, for example when a player walks out of the range.
Edited on 16 April 2015 - 05:47 PM
Dog #5
Posted 16 April 2015 - 07:52 PM
You're right. You cant use #players, because # only works on tables, and nil is not a table.

'#' also works on strings, and only on ordered tables (not sparse tables)…but you're right, it'll error on nil
flaghacker #6
Posted 16 April 2015 - 07:57 PM
'#' also works on strings, and only on ordered tables (not sparse tables)…but you're right, it'll error on nil

Yea I know, just wanted to keep it simple…
Dog #7
Posted 16 April 2015 - 07:58 PM
Apologies if I confused the issue…I pedantically tried to correct what I saw as misinformation - it wasn't my intent to de-simplify :)/>
Edited on 16 April 2015 - 06:01 PM
flaghacker #8
Posted 16 April 2015 - 08:01 PM
Apologies if I confused the issue…it was my intent to correct misinformation, not de-simplify :P/>/>

I think you won't get banned this time, but be careful in the future! :P/>
Dog #9
Posted 16 April 2015 - 08:17 PM
I'll watch my step :P/>

Getting back on track - @niels2398 - as flaghacker said, your second suggestion will work (and you should store the value returned by p.getPlayers() in a variable).

Since I got us off track, I'd like to reiterate what flaghacker already recommended…

players = p.getPlayers() --# so we only call p.getPlayers() once, but can make repeated use of the returned values
if players == nil then
  print("No players in range")
else
  --# rest of chkPlayer function here
end
Edited on 16 April 2015 - 06:28 PM