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

[OpenPeriphalAddons Sensor] attempt to index ? (a nil value)

Started by Zinne, 22 August 2016 - 05:31 PM
Zinne #1
Posted 22 August 2016 - 07:31 PM
Hello,

today I wanted to make a player sensor that only puts out a redstone signal when a player is near.
But before even getting to the point to put out the redstone signal I ran into an error.
It's this error:


The sensor is attached to the computer:


I have no idea where this could come from.
This is my source:

local outputSide = "top"
local periphalSide = "right"
local sensor = periphal.wrap(periphalSide)
while true do
print("1")
for _, name in pairs(sensor.getPlayerNames()) do
  print("2")
end
end

I am playing on the FTB Infinity Evolved Modpack (2.6.0)


Greetings,
Zinne
Zinne #2
Posted 22 August 2016 - 08:02 PM
I found the error myself. I didn't type "peripheral.wrap" - my bad.

Now I've been working more on it and ran into a new error immediatly:


This is the source:
local whitelistedUsers = {["Zinne91"] = true}
local outputSide = "top"
local peripheralSide = "right"
local doorState = false
local sensor = peripheral.wrap(peripheralSide)

while true do
	print("1")
	for _, name in pairs(sensor.getPlayerNames()) do
	  print("2")
	end
end
KingofGamesYami #3
Posted 22 August 2016 - 08:11 PM
Considering the error refers to line 9 and your script is only 9 lines long, and the ninth line isn't going to throw that error, I think there may be a substantial difference between the script you are running and the script you have posted.

Because the computer did not print either 1 or 2, I can conclude the error occurred before those lines.

There's only one line in which you are indexing a value, and that is line 3. You misspelled "peripheral" as "periphal".
Bomb Bloke #4
Posted 23 August 2016 - 02:10 AM
Now I've been working more on it and ran into a new error immediatly:

As Yami points out, the code you're showing us isn't matching up with the code you're running. The line numbers specified by your errors don't point where they should.

In your second code snippet, there are four instances where you attempt to call a function - first where you call "peripheral.wrap", then when you call "print", then "sensor.getPlayerNames", then "print" again.

Of the four (and assuming there aren't any typos in your original file), only "getPlayerNames" might not exist. Check to see which functions are accessible to your sensor.

http://www.computercraft.info/forums2/index.php?/topic/25280-opdoc-gui-documentation-browser-for-openperipheral/
Zinne #5
Posted 23 August 2016 - 07:08 AM
Now I've been working more on it and ran into a new error immediatly:
As Yami points out, the code you're showing us isn't matching up with the code you're running. The line numbers specified by your errors don't point where they should.
In your second code snippet, there are four instances where you attempt to call a function - first where you call "peripheral.wrap", then when you call "print", then "sensor.getPlayerNames", then "print" again.
Of the four (and assuming there aren't any typos in your original file), only "getPlayerNames" might not exist. Check to see which functions are accessible to your sensor.
http://www.computercraft.info/forums2/index.php?/topic/25280-opdoc-gui-documentation-browser-for-openperipheral/
Thanks for that. Didn't even know that the function doesn't exist anymore.

After working with that I found a problem I couldn't solve myself (that's why I'm here ay?)
This is what my code looks:

local whitelistedUsers = {["Zinne91"] = true}
local outputSide = "top"
local peripheralSide = "right"
local doorState = false
local sensor = peripheral.wrap(peripheralSide)

while true do
	for _, name in pairs(sensor.getPlayers()) do
		print(sensor.getPlayers())
		if (whitelistedUsers[name]) then
			rs.setOutput("top",true)
		end
	end
end

I tried to print everything but all I get is:


So how can I find out what names are in those tables? And does my whitelist function even work?
Bomb Bloke #6
Posted 23 August 2016 - 10:08 AM
Maybe? If you printed "name" instead of "sensor.getPlayers()", it'd be rather more obvious.
Zinne #7
Posted 23 August 2016 - 10:18 AM
I've done this now:
local whitelistedUsers = {["Zinne91"] = true}
local outputSide = "top"
local peripheralSide = "right"
local doorState = false
local sensor = peripheral.wrap(peripheralSide)
while true do
for _, name in pairs(sensor.getPlayers()) do
print(textutils.serialize(sensor.getPlayers()))
end
end
And it prints the following:

How can I extract the name of that now so that I can compare if it is on the whitelist?
Sorry for asking those (kinda) dumb questions, I'm prettyx new to lua and cc.
Edited on 23 August 2016 - 08:19 AM
KingofGamesYami #8
Posted 23 August 2016 - 01:06 PM

for i, player in pairs( sensor.getPlayers() ) do
  print( player.name )
end
Edited on 23 August 2016 - 11:06 AM