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

Player Detector Problems

Started by dobbylego, 22 May 2013 - 09:51 PM
dobbylego #1
Posted 22 May 2013 - 11:51 PM
When I run this startup program and I click on the block the program runs flawlessly but as soon as someone else not dobbylego or steve921 clicks it the program also runs like it was the correct person. Does anyone know how to make it so only specified people can make the program run?


	local i = 1
	
	function open()
	  rs.setBundledOutput("left", colors.orange)
	  sleep(.4)
	  rs.setBundledOutput("left", 0)
	  sleep(.4)
	end
	
	function close()
	  rs.setBundledOutput("left", colors.white)
	  sleep(.4)
	  rs.setBundledOutput("left", 0)
	  sleep(.4)
	end
	
	function elevatorUp()
	  rs.setBundledOutput("left", colors.magenta)
	  sleep(.4)
	  rs.setBundledOutput("left", 0)
	  sleep(.4)
	end
	
	peripheral.wrap("right")
	
	while true do
	  event, playerName = os.pullEvent("player")
	  print(playerName)
		if playerName == ("dobbylego") or ("steve921") then
--This is the part that runs when the correct people click the player detector.
		  print("Accepted")
		  open()
		  open()
		  open()
		  sleep(2.0)
		  close()
		  close()
		  close()
		else
--This is what runs when the wrong person click the player detector.
		  print("Denyed")
		  repeat
			elevatorUp()
			i = i + 1
		  until i == 13
		end
	  end
theoriginalbit #2
Posted 23 May 2013 - 12:24 AM
with if statements we cannot say this
if playerName == ("dobbylego") or ("steve921")
we must instead say this
if playerName == "dobbylego" or playerName == "steve921"

the reason is, that because of the way programming works, particularly in Lua, a value results in true, and you're giving that if statement a value i.e. "steve921". So even if the first conditional in the statement is false, the entire thing will return true because of the following rule with boolean OR logic.

true OR true = true
true OR false = true
false OR true = true
false OR false = false
dobbylego #3
Posted 23 May 2013 - 12:40 AM
Ok cool thanks! So it was bacsicly saying that if it is clicked then its runing the program? Thanks!
Oh one last thing.. is there a way to make it so that it can read names off of a list in another program for all of the people alowed? Would it just be
if playerName == "dobbylego"
if playerName == "name1"
if playerName == "name2"
LordIkol #4
Posted 23 May 2013 - 12:52 AM
Yes that's possible.

Have a look at the FS Api in the wiki to see how you can write files and read them.
theoriginalbit #5
Posted 23 May 2013 - 12:54 AM
yes it is possible by using tables.

this is a method using the same program…

if you define something like this at the top of the program

allowedPlayers = {
  ["dobbylego"] = true,
  ["name1"] = true,
  ["name2"] = true,
  -- etc, etc, etc ... just make sure you have == true
}

then make your if statement to this

if allowedPlayers[playerName] then

to use another program you would want to look into using os.loadAPI and then you can access all variables and functions from that program that aren't marked local.
dobbylego #6
Posted 23 May 2013 - 01:15 AM
Thank you! Both of you! It is working flawlessly!