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

Help with modifying my code!

Started by PetruZ, 12 May 2012 - 07:35 AM
PetruZ #1
Posted 12 May 2012 - 09:35 AM
Hi.
I've created a simple "password-unlock-door code" and I want to make it better.

I am playing on a multiplayer server and some of them knows about CTRL + T and therefore I have the code

os.pullEvent = os.pullEventRaw
on the top of my code. But what I want to do is like if I type in a specific password, it will simulate me pressing CTRL T somehow, even with that code on top.

Here is my full code:

os.pullEvent = os.pullEventRaw
function pass()
print "Welcome PetruZ!"
print "Please enter your password."
t = read("*")
if t == "password" then
print "Correct, welcome in!"
sleep(1)
redstone.setOutput("left", true)
sleep(2)
redstone.setOutput("left", flase)
print "Restarting..."
sleep(1)
os.shutdown()
pass()
else
print "Wrong password, shutting down!"
sleep(1)
os.shutdown()
pass()
end
end
pass()

Please help :)/>/> Thanks.
Luanub #2
Posted 12 May 2012 - 09:42 AM
Instead of using os.pullEvent = os.pullEventRaw you can create a new os.pullEvent function that contains an if statement. Keep in mind this will overwrite the global so you will need to restore it in order to get it back to the original functionality.

replace os.pullEvent = os.pullEventRaw with:

function os.pullEvent()
	local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
	if event == "terminate" and password == "123abc" then
	 print ("Terminated")
   	 error()
	end
	return event, p1, p2, p3, p4, p5
end
PetruZ #3
Posted 12 May 2012 - 09:53 AM
Hmm, if I got it right, I put that code on the top of my code. I did that tried typing the password (123abc) but it just followed my original code, meaning "Wrong password…".

I'm not quite sure how to use that code. Do I have to do something else than type in "123abc" ?
Sorry for my bad coding skills.

Thanks.
Luanub #4
Posted 12 May 2012 - 10:43 AM
Sorry I read your post wrong. Leave the code as it was originally with the os.pullEvent = os.pullEventRaw.

Is what you will want to do is add in an elseif statement to your password checks that looks like this.

elseif password == "123abc" then
   print ("Terminated")
   error()


change password to your user input var, and 123abc to whatever you want that password to be.

So finished code
Spoiler

os.pullEvent = os.pullEventRaw
function pass()
print "Welcome PetruZ!"
print "Please enter your password."
t = read("*")
if t == "password" then
print "Correct, welcome in!"
sleep(1)
redstone.setOutput("left", true)
sleep(2)
redstone.setOutput("left", flase)
print "Restarting..."
sleep(1)
os.shutdown()
pass()
elseif t == "123abc" then -- change 123abc to whatever you choose
   print ("Terminated")
   error()
else
print "Wrong password, shutting down!"
sleep(1)
os.shutdown()
pass()
end
end
pass()

Edited on 12 May 2012 - 08:45 AM
PetruZ #5
Posted 12 May 2012 - 10:51 AM
Thank you, that works :)/>/>