Jump to content




Attempt to index ? (a nil value)


  • You cannot reply to this topic
2 replies to this topic

#1 gheotic

  • Members
  • 64 posts
  • LocationDenmark

Posted 27 October 2015 - 08:10 AM

Where is the problem in this program?
Here is a link to the documentation for the peripheral i use.


local p = peripheral.wrap("left")
while true do
   sleep(0.5)
   if p.getNearbyPlayers(5)[1] == nil then
	  redstone.setOutput("bottom", false)
   else
	  name = p.getNearbyPlayers(5)[1]["player"] --This is where it fails
	  if name == "tilde1000" then
		 redstone.setOutput("bottom", true)
	   end
   end
end  


#2 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 27 October 2015 - 09:49 AM

If this bit is erroring:
p.getNearbyPlayers(5)[1]["player"]

Then one of these is nil:
p.getNearbyPlayers(5)
p.getNearbyPlayers(5)[1]

The easiest thing to do would be to check each one to see if it is nil:
local players = p.getNearbyPlayers(5)
if players ~= nil and players[1] ~= nil then
  print(players[1]["player"])
end

You might also want to loop through every possible player:
for _, player in ipairs(p.getNearbyPlayers(5)) do
  print(player["player"])
end

Edited by SquidDev, 27 October 2015 - 09:49 AM.


#3 gheotic

  • Members
  • 64 posts
  • LocationDenmark

Posted 27 October 2015 - 11:10 AM

View PostSquidDev, on 27 October 2015 - 09:49 AM, said:

If this bit is erroring:
p.getNearbyPlayers(5)[1]["player"]

Then one of these is nil:
p.getNearbyPlayers(5)
p.getNearbyPlayers(5)[1]

The easiest thing to do would be to check each one to see if it is nil:
local players = p.getNearbyPlayers(5)
if players ~= nil and players[1] ~= nil then
  print(players[1]["player"])
end

You might also want to loop through every possible player:
for _, player in ipairs(p.getNearbyPlayers(5)) do
  print(player["player"])
end

Thanks works like charm!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users