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

password lock

Started by bradster2214, 23 August 2017 - 09:35 AM
bradster2214 #1
Posted 23 August 2017 - 11:35 AM
i have created a basic password lock and i want to add a password change function.
line 15 is my problem. i want to set the input after line 14 as a variable and i don't know how to do that. please help me! btw i'm creating a door lock for a restricted area in a rocket silo with the galacticraft mod :)/>



pastebin get 9TE9jLPY
https://pastebin.com/9TE9jLPY
KingofGamesYami #2
Posted 23 August 2017 - 01:16 PM
You will have to store the password in a file, or it won't persist across multiple instances of the program. I recommend using the fs API to do this. You will have to read the file at the beginning of the program (after checking that it exists!) then compare the contents to the user's input. When the password is changed, you'll have to write the new password to the file.
bradster2214 #3
Posted 27 August 2017 - 05:56 AM
there a tutorial or something on this? the first time i used this i did a very basic code. i understand that this is still considered basic as people have made whole operating systems but i would love to know if there is a tutorial on it
KingofGamesYami #4
Posted 27 August 2017 - 01:54 PM
I doubt there's a tutorial for this specific of a program, but there are tutorials for the individual parts. Don't hesitate to ask questions here, we're glad to help :)/>/>

Here's one on fs.

The wiki is also a great resource, here's the page on fs.
Edited on 27 August 2017 - 12:04 PM
Dave-ee Jones #5
Posted 28 August 2017 - 05:39 AM
The fs API is definitely what you're looking for here. Consider this to be at the start of your program:

local file = fs.open("/password","r")
local password = file.read()
file.close()
Line by line explanation:
1. Opens the "/password" file (/ means root directory), with the parameter "r" meaning "read mode", allowing you to read the contents
2. This sets the variable "password" to the contents of the file
3. Closes the file, saying you're finished with it. If you don't do this you will have errors later and won't be able to open/close the file again until you reboot the computer.

Consider this to be where you want to have a new password:

local newPassword = read("*")
local file = fs.open("/password","w")
file.write(newPassword)
file.close()
Line by line explanation:
1. Set "newPassword" to what the user puts in
2. Open the "/password" file (like before) except this time we're opening it with parameter "w", meaning "write mode", allowing us to over-write what's currently in the file
3. Write the value of "newPassword" to the file
4. Close the file

I hope this helps you understand what the fs API does :)/>
Edited on 28 August 2017 - 10:11 PM
Lupus590 #6
Posted 28 August 2017 - 03:30 PM

local file = fs.open("/password","r")
local password = fs.read()
file.close()

file.read()not fs.read()
Dave-ee Jones #7
Posted 29 August 2017 - 12:11 AM

local file = fs.open("/password","r")
local password = fs.read()
file.close()

file.read()not fs.read()

Thankin' yo'. I always do that, even when I go to run a program and I'm looking for the error I facepalm everytime..