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

Open Peripherals Sensor, Detect Player and act depending on who it is

Started by Molo, 12 May 2014 - 01:47 PM
Molo #1
Posted 12 May 2014 - 03:47 PM
Hello,

Trying to use openperipherals sensor to detect nearby players, and then print them out to the computer.

Here comes the code:

m = peripheral.wrap("back")
local acceptedPlayer = {}

acceptedPlayer["thePlayersName"] = true

function accepted(playerName)
		m.clear()
		m.setCursorPos(5,5)
		m.write("Welcome "..playerName.." to my Place!")
		m.setCursorPos(5,6)
		m.write("You are accepted!")
		sleep(2)
end

while true do
		sleep(1)
		players = p.getPlayerNames()
		for num,name in pairs(players) do

		if acceptedPlayer[name] then
		  print("Correct player located")
		  accepted()
	  
		else
		  print("No one here")
		end
end
end

And now to the problem, it prints out the following:


mini:10: attempt to concatenate nil and string

I'm understanding it's complaining on the "..playerName..", but why?
Can't it find the name or what's the problem?
I'm pretty basic at this and have been looking in other peoples codes trying to make my own version, so please explain like I'm a noob, prefered a solution :)/>
Edited on 12 May 2014 - 06:08 PM
Lyqyd #2
Posted 12 May 2014 - 03:53 PM
You aren't passing the name to your accepted function, so playerName is nil.
Molo #3
Posted 12 May 2014 - 04:02 PM
You aren't passing the name to your accepted function, so playerName is nil.

Could you please give an example of how to pass on the names?
Lyqyd #4
Posted 12 May 2014 - 04:04 PM
Replace the call to accepted() with accepted(name).
Molo #5
Posted 12 May 2014 - 04:06 PM
Replace the call to accepted() with accepted(name).
Oh it was that simple, thanks a bunch!
Molo #6
Posted 12 May 2014 - 08:02 PM
Hello again, code is almost done now, one small (big?) problem left though.
Lets say there is a "whitelisted" and a "blacklisted" player in the room.
The redstone signal will then come on and off because the system dont know which one to choose from.
I want it to be that if there is only a whitelisted player in the area, open the door and let it stay open until he leaves or a blacklisted/stranger comes into the room.

So Whitelisted player only = Open
Whitelisted + Blacklisted / Stranger = Closed
Blacklisted + Stranger = Closed

Here comes the code:


p = peripheral.wrap("left")
m = peripheral.wrap("monitor_1")
local whiteList = {}
local blackList = {}

--whiteList["Player1"] = true
whiteList["Player1"] = true
blackList["Player2"] = true

function centerText(text)
m.setTextScale(0.5)
x,y = m.getSize()
x1,y1 = m.getCursorPos()
m.setCursorPos((math.floor(x/2) - (math.floor(#text/2))), y1)
m.write(text)
end

function white(playerName)
m.clear()
y2 = 3
m.setCursorPos(1, y2)
centerText("Hej "..playerName.."!")
m.setCursorPos(1, y2 +1)
centerText("Välkommen till Player1's & Player2's place!")
m.setCursorPos(1, y2 +2)
centerText("Du är Accepterad att komma in i denna bas.")
m.setCursorPos(1, y2 +3)
centerText("Vänligen ta Elevatorn ner till höger.")
rs.setBundledOutput("bottom", 1)
sleep(2)
rs.setBundledOutput("bottom", 0)
end

function black(playerName)
m.clear()
y2 = 3
m.setCursorPos(1, y2)
centerText("Hej "..playerName.."!")
m.setCursorPos(1, y2 +1)
centerText("Du är inte välkommen här.")
m.setCursorPos(1, y2 +2)
centerText("Vänligen stick härifrån.")
m.setCursorPos(1, y2 +3)
centerText("Portal finns bakom dig.")
end


function nobody(playerName)
m.clear()
y2 = 3
m.setCursorPos(1, y2)
centerText("Hej "..playerName.."!")
m.setCursorPos(1, y2 +1)
centerText("Du är varken nekad eller godkänd för åtkomst till denna bas.")
m.setCursorPos(1, y2 +2)
centerText("Vänligen kontakta Player1 eller Player2.")
m.setCursorPos(1, y2 +3)
centerText("Portal till Spawn finns bakom dig.")
end

function reset()
rs.setBundledOutput("bottom", 0)
end

while true do
sleep(1)
players = p.getPlayerNames()
for num,name in pairs(players) do

if whiteList[name] then
print("Vitlistad spelare: "..name.."")
white(name)

elseif blackList[name] then
print("Blacklistad spelare: "..name.."")
black(name)

else
print("Systemet känner inte igen: "..name.."")
nobody(name)
end
rs.setBundledOutput("bottom", 0)
end
end

I'm guessing it's possible, I just don't know how.

EDIT: Also can't get the Tab's to work in the code like the first post, don't know what I'm doing different this time.
Edited on 12 May 2014 - 06:23 PM
Molo #7
Posted 16 June 2014 - 07:41 PM
Hello again, code is almost done now, one small (big?) problem left though.
Lets say there is a "whitelisted" and a "blacklisted" player in the room.
The redstone signal will then come on and off because the system dont know which one to choose from.
I want it to be that if there is only a whitelisted player in the area, open the door and let it stay open until he leaves or a blacklisted/stranger comes into the room.

So Whitelisted player only = Open
Whitelisted + Blacklisted / Stranger = Closed
Blacklisted + Stranger = Closed

Here comes the code:


p = peripheral.wrap("left")
m = peripheral.wrap("monitor_1")
local whiteList = {}
local blackList = {}

--whiteList["Player1"] = true
whiteList["Player1"] = true
blackList["Player2"] = true

function centerText(text)
m.setTextScale(0.5)
x,y = m.getSize()
x1,y1 = m.getCursorPos()
m.setCursorPos((math.floor(x/2) - (math.floor(#text/2))), y1)
m.write(text)
end

function white(playerName)
m.clear()
y2 = 3
m.setCursorPos(1, y2)
centerText("Hej "..playerName.."!")
m.setCursorPos(1, y2 +1)
centerText("Välkommen till Player1's & Player2's place!")
m.setCursorPos(1, y2 +2)
centerText("Du är Accepterad att komma in i denna bas.")
m.setCursorPos(1, y2 +3)
centerText("Vänligen ta Elevatorn ner till höger.")
rs.setBundledOutput("bottom", 1)
sleep(2)
rs.setBundledOutput("bottom", 0)
end

function black(playerName)
m.clear()
y2 = 3
m.setCursorPos(1, y2)
centerText("Hej "..playerName.."!")
m.setCursorPos(1, y2 +1)
centerText("Du är inte välkommen här.")
m.setCursorPos(1, y2 +2)
centerText("Vänligen stick härifrån.")
m.setCursorPos(1, y2 +3)
centerText("Portal finns bakom dig.")
end


function nobody(playerName)
m.clear()
y2 = 3
m.setCursorPos(1, y2)
centerText("Hej "..playerName.."!")
m.setCursorPos(1, y2 +1)
centerText("Du är varken nekad eller godkänd för åtkomst till denna bas.")
m.setCursorPos(1, y2 +2)
centerText("Vänligen kontakta Player1 eller Player2.")
m.setCursorPos(1, y2 +3)
centerText("Portal till Spawn finns bakom dig.")
end

function reset()
rs.setBundledOutput("bottom", 0)
end

while true do
sleep(1)
players = p.getPlayerNames()
for num,name in pairs(players) do

if whiteList[name] then
print("Vitlistad spelare: "..name.."")
white(name)

elseif blackList[name] then
print("Blacklistad spelare: "..name.."")
black(name)

else
print("Systemet känner inte igen: "..name.."")
nobody(name)
end
rs.setBundledOutput("bottom", 0)
end
end

I'm guessing it's possible, I just don't know how.

EDIT: Also can't get the Tab's to work in the code like the first post, don't know what I'm doing different this time.

Been a while now, still no luck in figuring it out.
Anyone here up for helping me? :)/>
Bomb Bloke #8
Posted 17 June 2014 - 02:03 AM
Maybe something like this:

local blacklisted, whitelisted = false, false

for num,name in pairs(players) do
	if whiteList[name] then
		print("Vitlistad spelare: "..name.."")
		whitelisted = true
	elseif blackList[name] then
		print("Blacklistad spelare: "..name.."")
		blacklisted = true
	else
		print("Systemet känner inte igen: "..name.."")
		nobody(name)
	end
end

if whitelisted and not blacklisted then
	rs.setBundledOutput("bottom", 1)
else
	rs.setBundledOutput("bottom", 0)
end
Molo #9
Posted 17 June 2014 - 04:15 PM
Maybe something like this:

local blacklisted, whitelisted = false, false

for num,name in pairs(players) do
	if whiteList[name] then
		print("Vitlistad spelare: "..name.."")
		whitelisted = true
	elseif blackList[name] then
		print("Blacklistad spelare: "..name.."")
		blacklisted = true
	else
		print("Systemet känner inte igen: "..name.."")
		nobody(name)
	end
end

if whitelisted and not blacklisted then
	rs.setBundledOutput("bottom", 1)
else
	rs.setBundledOutput("bottom", 0)
end

Alright, I combined a bit of your code into mine and it seems to be working flawless!
Right now it only checks the "whitelisted" and "blacklisted" part in the following part of the code:

if whitelisted and not blacklisted then
	    rs.setBundledOutput("bottom", 1)
else
	    rs.setBundledOutput("bottom", 0)
end
Would it be possible to add a third or maybe even fourth stance?
Like:
whitelisted + unknown (function nobody(name))
and also:
blacklisted + unknown

Thanks for helping me! :D/>
Edited on 17 June 2014 - 02:36 PM
Bomb Bloke #10
Posted 17 June 2014 - 04:35 PM
Sure, stick a sleep after that last "if" block, just before the last "end" in the script. Five seconds should be suitable.

blacklisted / whitelisted need to be set to false before the "for" loop runs, every time before it runs.
Molo #11
Posted 17 June 2014 - 04:38 PM
Sure, stick a sleep after that last "if" block, just before the last "end" in the script. Five seconds should be suitable.

blacklisted / whitelisted need to be set to false before the "for" loop runs, every time before it runs.

Alright, thanks!

One last question, hopefully.

I combined a bit of your code into mine and it seems to be working flawless!
Right now it only checks the "whitelisted" and "blacklisted" part in the following part of the code:

if whitelisted and not blacklisted then
		rs.setBundledOutput("bottom", 1)
else
		rs.setBundledOutput("bottom", 0)
end
Would it be possible to add a third or maybe even fourth stance?
Kinda hard to explain but I want it to check if there's a whitelisted and blacklisted player like it already does, but also to check for a unknown player.
If there's only whiteListed players, then just rs.setBundledOutput("bottom", 1)
But if there's any blackListed or unknown players then rs.setBundledOutput("bottom", 0)

Hope I explained good enough :3

Thanks for helping me! :D/>
Edited on 17 June 2014 - 02:41 PM
Bomb Bloke #12
Posted 17 June 2014 - 04:55 PM
I figured you wanted it to open if a white listed player was present, unless a black listed player was present, while ignoring strangers entirely.

If you want it to only open when a white listed player is present on their own, stranger or no, then why have a black list at all?

You'd just do this:

local blacklisted, whitelisted = false, false  -- To be clear, this needs to be sitting just above the "for" loop!

for num,name in pairs(players) do
        if whiteList[name] then
                print("Vitlistad spelare: "..name.."")
                whitelisted = true
        else
                print("Blacklistad spelare: "..name.."")
                blacklisted = true
        end
end
Molo #13
Posted 17 June 2014 - 05:16 PM
EDIT: Hmm, I think I'm something on the way, I'll update here later.
Edited on 17 June 2014 - 04:03 PM
Molo #14
Posted 18 June 2014 - 01:37 PM
I figured you wanted it to open if a white listed player was present, unless a black listed player was present, while ignoring strangers entirely.

If you want it to only open when a white listed player is present on their own, stranger or no, then why have a black list at all?

You'd just do this:

local blacklisted, whitelisted = false, false  -- To be clear, this needs to be sitting just above the "for" loop!

for num,name in pairs(players) do
		if whiteList[name] then
				print("Vitlistad spelare: "..name.."")
				whitelisted = true
		else
				print("Blacklistad spelare: "..name.."")
				blacklisted = true
		end
end
Hello again,

I think the system is working pretty good right now. (Haven't been able to test for 100% but I think it's working)
But I got 2 new SMALL problems.
First one is that whenever there's no player there, the computer keeps spamming "Welcome XXX", nothing else, just that. (Code below)
Also, when the gate has opened for whitelisted players, it does't close after, can that be fixed someway?

This is the most "important" part of the code I guess.

while true do
sleep(1)
players = p.getPlayerNames()
for num,name in pairs(players) do

if whiteList[name] then
print("Vitlistad spelare: "..name.."")
whitelisted = true
white(name)

elseif blackList[name] then
print("Blacklistad spelare: "..name.."")
blacklisted = true
black(name)

else
print("Systemet känner inte igen: "..name.."")
unknown = true
nobody(name)
end
rs.setOutput("bottom", false)
end

if unknown then
rs.setOutput("bottom", false)
		print("Unknown combination found, closing gate")
elseif whitelisted and not blacklisted and not unknown then
rs.setOutput("bottom", true)
		print("Welcome XXX")
elseif blacklisted and not whitelisted and not unknown then
rs.setOutput("bottom", false)
print("Actions taken")
else
		rs.setOutput("bottom", false)
		print("Error: WTF IS GOING ON")
end
end
Edited on 18 June 2014 - 11:43 AM
Bomb Bloke #15
Posted 18 June 2014 - 02:07 PM
while true do
	sleep(5) -- This needs to be set to a sufficient duration for players to get through the door.
	players = p.getPlayerNames()
	
	local whitelisted, blacklisted, unknown = false, false, false  -- "Just above the for loop"
	
	for num,name in pairs(players) do
		if whiteList[name] then
			print("Vitlistad spelare: "..name.."")
			whitelisted = true
			white(name)  -- Make sure this function doesn't change redstone state.
		elseif blackList[name] then
			print("Blacklistad spelare: "..name.."")
			blacklisted = true
			black(name)  -- Make sure this function doesn't change redstone state.
		else
			print("Systemet känner inte igen: "..name.."")
			unknown = true
			nobody(name) -- Make sure this function doesn't change redstone state.
		end
	end

	if blacklisted then
		rs.setOutput("bottom", false)
		print("Actions taken")
	elseif unknown then
		rs.setOutput("bottom", false)
		print("Unknown combination found, closing gate")
	elseif whitelisted then
		rs.setOutput("bottom", true)
		print("Welcome XXX")
	else
		rs.setOutput("bottom", false)
	end
end