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

couple questions.

Started by subzero22, 29 October 2014 - 02:28 AM
subzero22 #1
Posted 29 October 2014 - 03:28 AM
First off I'm on tekkit classic normally do direwolf20 but a friend just started mc and wanted to go on classic lol.

Why doesn't this work

if id == 7 or id == 15 and message == "close" then
do stuff
end

I got it working by doing this but why doesn't the top one work?

if id == 7 or id == 15 then
if message == "close" then
do stuff
end
end

My other question is I used to have a program running while at the same time have a passward program going. Kinda like have a server program running but if someone trieds to use the server computer will ask them a password before server shuts down so you can do stuff in it. I forgot how I did this and wondering how to do it again.

As in on the server computer itself if you click on it, it will ask you for a password but at the same time it will take/send commands from other ocmputers untill you enter the password to shut down the server script so you can work on it. this way someone can't just come and terminate it and edit it.
Saldor010 #2
Posted 29 October 2014 - 03:36 AM
Not sure about the first question, but to answer your password program/regular program running at the same time question, you can use the parallel API.

And just do something like this.


function Program()
  ...
end
function Password()
  ..
end
parallel.waitForAny(Program,Password)
Dragon53535 #3
Posted 29 October 2014 - 03:52 AM
The first one is easy.

With the code you have, the computer sees this

if (id == 7) or (id == 15 and message == "close") then
In which case if the id is 7, and the message is whatever, then go on, or if the id is 15 and it's sending the message close, then go on.
You want it like this, and type it like this

if (id == 7 or id == 15) and message == "close" then
That way the computer is like, okay we're going to pair this 'and' with the result of the 'or', rather than the 'and' with the second option in the 'or'.

Edit: Afterthought, you might be able to get away with an 'and or' rather than the 'or and' you have.

Not sure if it's going to exactly work this way, however the way above will work

if message == "close" and id == 7 or id == 15 then

Nope just tested doesn't work D:

I'm an idiot, wrote my test code incorrectly and thus didn't get correct result. Yes this way does work.
As my college professor said, most errors in code are logic errors…
Edited on 29 October 2014 - 03:00 AM
Cycomantis #4
Posted 29 October 2014 - 03:53 AM
You need to check what the message is for both ID's.

if id == 7 and message == "close" or id == 15 and message == "close" then
do stuff
end
Edited on 29 October 2014 - 02:54 AM
Bomb Bloke #5
Posted 29 October 2014 - 07:23 AM
You need to check what the message is for both ID's.

Nah, that's really not necessary.

As my college professor said, most errors in code are logic errors…

And I guess that means the rest are illogical errors. That covers the spectrum pretty well, I reckon.
Edited on 29 October 2014 - 08:50 AM
subzero22 #6
Posted 29 October 2014 - 09:21 AM
ok so all I had to do was add the ( and ) or am I missing something here?
Bomb Bloke #7
Posted 29 October 2014 - 09:52 AM
Personally, I'd use this line Dragon gave you:

if (id == 7 or id == 15) and message == "close" then

Easier for me to read later, given that I can never remember the order of operations in such cases. The brackets let you enforce 'em any way you want 'em.
subzero22 #8
Posted 29 October 2014 - 10:51 AM
ah ok thanks