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

How To Create A Player Detector Door?

Started by austinv11, 28 September 2013 - 05:32 PM
austinv11 #1
Posted 28 September 2013 - 07:32 PM
I am trying to create a door that detects which player opened it, and records that in a log. I have had no experience using wired modems, player detectors, and creating documents in a computer ( I know, that's a lot). Please help!

Any response is appreciated! Thanks in advance!
Lyqyd #2
Posted 28 September 2013 - 09:34 PM
You should post your current code. If you don't have any code, you should ask more specific questions about what you need to know.

You should also read some of the documentation on the wiki.
Grim Reaper #3
Posted 29 September 2013 - 12:37 AM
Once you've placed the player detector next to the door, you'll need to check for an event to occur. The event triggered by player detectors is "player". This allows you to open the door when a particular player, or any player, has activated the player detector and record the player's name.

Here's a script that opens the door located on 'doorSide' and writes the players name to the file located at 'logFilePath'.
Spoiler

local DOOR_OPEN_DURATION = 0.5 -- The number of seconds the door will stay open.

local doorSide    = "left"
local logFilePath = "/doorOpenLog"

-- Opens or creates the file at logFilePath and appends
-- playerName to the end of the file.
-- Returns true if the writing succeeded; false if not.
local function logPlayerName (logFilePath, playerName)
    local fileHandle = fs.open (logFilePath, 'a')

    if fileHandle then
        fileHandle.writeLine (playerName)
        fileHandle.close()

        return true
    end

    return false
end

-- Opens the door for DOOR_OPEN_DURATION seconds,
-- then closes the door.
local function openDoor()
    rs.setOutput (doorSide, true)
    sleep (DOOR_OPEN_DURATION)
    rs.setOutput (doorSide, false)
end

-- Waits infinitely for player events from the player detector.
-- When a player is detected, the door is opened and the name
-- of the player is recorded in logFilePath.
local function main (logFilePath)
    while true do
        -- Change this to os.pullEventRaw if you want to avoid termination.
        local _, playerName = os.pullEvent ("player")

        openDoor()
        logPlayerName (logFilePath, playerName)
    end
end

main()
austinv11 #4
Posted 29 September 2013 - 08:18 AM
Once you've placed the player detector next to the door, you'll need to check for an event to occur. The event triggered by player detectors is "player". This allows you to open the door when a particular player, or any player, has activated the player detector and record the player's name.

Here's a script that opens the door located on 'doorSide' and writes the players name to the file located at 'logFilePath'.
Spoiler

local DOOR_OPEN_DURATION = 0.5 -- The number of seconds the door will stay open.

local doorSide	= "left"
local logFilePath = "/doorOpenLog"

-- Opens or creates the file at logFilePath and appends
-- playerName to the end of the file.
-- Returns true if the writing succeeded; false if not.
local function logPlayerName (logFilePath, playerName)
	local fileHandle = fs.open (logFilePath, 'a')

	if fileHandle then
		fileHandle.writeLine (playerName)
		fileHandle.close()

		return true
	end

	return false
end

-- Opens the door for DOOR_OPEN_DURATION seconds,
-- then closes the door.
local function openDoor()
	rs.setOutput (doorSide, true)
	sleep (DOOR_OPEN_DURATION)
	rs.setOutput (doorSide, false)
end

-- Waits infinitely for player events from the player detector.
-- When a player is detected, the door is opened and the name
-- of the player is recorded in logFilePath.
local function main (logFilePath)
	while true do
		-- Change this to os.pullEventRaw if you want to avoid termination.
		local _, playerName = os.pullEvent ("player")

		openDoor()
		logPlayerName (logFilePath, playerName)
	end
end

main()

Thanks! Just need to edit a few lines of code to better suit my preferences, otherwise nice job :)/> !