7 posts
Posted 06 June 2016 - 12:57 PM
Hi,
I have
sensor = peripheral.wrap("top")
sensor.getPlayers()
Now, how do I access the table that getPlayers() makes and then do an if whatever = shivy011 then redstone.setOutput("back") etc
I just want the player name, and maybe print the player name on the screen for debug purposes
Thanks in advance!
3057 posts
Location
United States of America
Posted 06 June 2016 - 02:11 PM
I would start by serializing the table and printing it to the screen, like so:
local sensor = peripheral.wrap( "top" ) --#local variables are good practice
print( textutils.serialize( sensor.getPlayers() ) )
That will give you a visual representation of the table, from which you can determine how to access the data you want.
7 posts
Posted 06 June 2016 - 02:33 PM
Thanks
I have uuid = "asodhsadf whatever"
name = "Shivy011"
however - how do I extract this "name" and do an if statement with it?
3057 posts
Location
United States of America
Posted 06 June 2016 - 02:38 PM
I assume it looks like this:
{
{
uuid="stuffs",
name="Shivy011",
}
}
In which case you would do this:
local sensor = peripheral.wrap( "top" )
for k, v in pairs( sensor.getPlayers() ) do --#iterate through the first table, which can contain multiple players
if v.name == "Shivy011" ) then --#if the name of the player in the current iteration is yours
--#do stuff
end
end
7 posts
Posted 06 June 2016 - 02:43 PM
I have just done;
local sensor = peripheral.wrap("top")
print(textutils.serialize(sensor.getPlayers()))
for k, v in pairs(sensor.getPlayers()) do
if v.name = "Shivy011" then
redstone.setOutput("back", true)
sleep(5)
end
end
i get
bios:14: [string "sensor"]:4: "then" expected
7083 posts
Location
Tasmania (AU)
Posted 06 June 2016 - 03:00 PM
Use = for assignments, and == for comparisons. If you stick an assignment operator into the middle of an "if"'s conditional statement, you'll get complaints.
7 posts
Posted 06 June 2016 - 03:11 PM
Don't worry, it's all working
Thank so much for your help!
Edited on 06 June 2016 - 01:26 PM
7 posts
Posted 06 June 2016 - 06:25 PM
Another issue
Code:
http://pastebin.com/rxkuJvujIf more than one player is around, the program doesn't open the door (i.e. me and another player.) To be honest I have no idea how to fix this!
Any ideas?
3057 posts
Location
United States of America
Posted 06 June 2016 - 11:23 PM
I'm not sure why that would happen. It should find you if you're within range.
The only issue I see is the program doesn't yield if nobody is in range, and would be killed by CC's yield protection.
7 posts
Posted 06 June 2016 - 11:38 PM
Hm, I am also finding that the redstone output gets stuck on a LOT and it just doesn't work properly. I think it may be to do with chunk loading, though the chunk the computer in should be loaded when I am testing it…
Could be a server issue?
218 posts
Location
tmpim
Posted 06 June 2016 - 11:44 PM
What is happening is that when you are there, it detects you and sets the redstone output to true to open the door, as expected. However, if there are two people, it will open the door, because it detected you, but then the loop continues and sees that someone else is there, it goes into the else part of the if statement because that person is not you. In the else block, you set the redstone output to false, closing the door.
Recap:
Sees you, opens door
Sees your friend, closes door
So it appears that it doesn't work at all, it does, just a little flaw in your logic.
To fix this, you need to stop the 'for' loop if it detects you.
Easy to do this, in your if statement, after you print, put a line that just says 'break'
if v.name == "Shivy011" then
redstone.setOutput("left", true)
sleep(2)
redstone.setOutput("left", false)
print("Shivy011 is here")
break ---This line here is new
else
Sidenote: What is the 'random' variable for?
Edited on 07 June 2016 - 01:22 PM
1080 posts
Location
In the Matrix
Posted 07 June 2016 - 01:12 AM
Incinerate you are incorrect, he has a sleep after the rs.setOutput so that it does update. Also, the break keyword will completely make it worse.
What I suspect the issue is that the other unauthorized player is getting detected first, and thus it's turning off the redstone, and then you get detected and it tries to turn it on, but it was already told to turn it off. I would add a sleep after the turning off the redstone in the incorrect part, as well as at the end of the while true loop as that's probably crashing your program.
So
print("Shivy001 not here")
sleep()
end
end
sleep()
end
Edited on 06 June 2016 - 11:13 PM
218 posts
Location
tmpim
Posted 07 June 2016 - 02:29 AM
Incinerate you are incorrect, he has a sleep after the rs.setOutput so that it does update. Also, the break keyword will completely make it worse.
What I suspect the issue is that the other unauthorized player is getting detected first, and thus it's turning off the redstone, and then you get detected and it tries to turn it on, but it was already told to turn it off. I would add a sleep after the turning off the redstone in the incorrect part, as well as at the end of the while true loop as that's probably crashing your program.
So
print("Shivy001 not here")
sleep()
end
end
sleep()
end
/offtopic
I saw the sleep a couple minutes after I posted it but I had to leave for dinner and could not change it, your solution might work, although it is confusing to me how the rs api would not override after being told to turn back on, maybe an issue with tick delay? Idk. Thanks for correcting me though!
Edit: Although my comment about adding the 'break' token, is not wrong. It would help for efficiencies sake.
Edited on 07 June 2016 - 12:30 AM
7083 posts
Location
Tasmania (AU)
Posted 07 June 2016 - 03:25 AM
If more than one player is around, the program doesn't open the door (i.e. me and another player.) To be honest I have no idea how to fix this!
Does it still print "Shivy011 is here"?
Personally I'd write the code along these lines:
local sensor = peripheral.wrap("right")
while true do
local found = false
for k, v in pairs(sensor.getPlayers()) do
if v.name == "Shivy011" then
found = true
break
end
end
redstone.setOutput("left", found)
print(found and "Shivy011 is here" or "Shivy011 not here")
sleep(2)
end
This way, the door should simply stay open so long as you're around, instead of constantly opening / closing all the time.