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

Having trouble with this,

Started by tacohaha, 26 February 2014 - 07:46 PM
tacohaha #1
Posted 26 February 2014 - 08:46 PM
I have been working on a code that is basically a player detector, that activates a redstone signal, and displays a message in chat via a chatbox and displays a message through terminal glasses, i have used this code for the terminal glasses part:
http://pastebin.com/1sWDJK3x/

Its posting the message to the glasses fine, what im having the problem with is the section of code that printing to the chat:
http://pastebin.com/n3iX9q6j/
The only part that is wrong with this is that it doesn't announce to the chat that someone not whitelisted has tried the door.
please help. :)/>
CometWolf #2
Posted 26 February 2014 - 09:09 PM
What peripheral are you using to detect the player? Also, keep in mind that the chat box has a default range of like 128 blocks in smp.
oeed #3
Posted 26 February 2014 - 09:11 PM
Could you be a bit more specific with your problem?

Does announcing anything to the chat work?

Also, this appears to be very redundant.


if event == "player" or event ~= "player" then

It's like saying, "If the car is red, or if it's not, then stop it." Whereas you should basically just say, "Stop every car."

In your case, either change the if requirements or remove it.
tacohaha #4
Posted 26 February 2014 - 09:12 PM
What peripheral are you using to detect the player? Also, keep in mind that the chat box has a default range of like 128 blocks in smp.
I am using the player detector from misc peripherals. And i was standing 3 blocks away from the chatbox when i tested it.

Could you be a bit more specific with your problem?

Does announcing anything to the chat work?

Also, this appears to be very redundant.

if event == "player" or event ~= "player" then

It's like saying, "If the car is red, or if it's not, then stop it." Whereas you should basically just say, "Stop every car."

In your case, either change the if requirements or remove it.
When anyone whitelisted uses the player detector, it works, only when someone that isnt white listed it doesent work, and the "if event == "player" or event ~= "player" then" is basically making the program tell me whenever someone tries to open it, whitelisted inside it or not
Bomb Bloke #5
Posted 26 February 2014 - 10:39 PM
… and the "if event == "player" or event ~= "player" then" is basically making the program tell me whenever someone tries to open it, whitelisted inside it or not
No, it's saying "whether the event was a player or not, do this". Which is a lengthy way of saying "just do this under any circumstance".

Part of your problem is that you've misplaced at least one "end". Here's the code with proper indentation, which'll hopefully make clear to you where they should be moved:

Spoiler
while true do
	event, p1 = os.pullEvent()

	if event == "player" then  -- Every line inside this block should assume "event" is "player".
		for i = 1, #players do
			if players[i] == p1 then
				openDoor()
				open = true
				if event ~= "player" then  -- Because we already know "event" is "player" while in this block, we also know this is always going to be false.
					chat.say("You are not authorized to use this.")
					open = false
				end
			end
		end

		-- How about a check HERE for those cases where we got through the whole "for" loop without finding a match between players[i] and p1?

		if event == "player" or event ~= "player" then
			shell.run("ccGlasses")
			print("Door Attempted By: "..p1)
			sleep(5)
			term.clear()
		end

		if event == "redstone" then
			openDoor()
		end


		if (event == "key") and (p1 == 16) then
			break
		end

	end
end

The other issue seems to be that you're not familiar with what data the "player" event is actually returning. I'm assuming it should come back with a table, but Comet's hinting that's peripheral-dependent. (Edit: Looks like the MiscPeripheral's one does indeed return just a string as the parameter.)
Edited on 26 February 2014 - 09:45 PM
tacohaha #6
Posted 27 February 2014 - 01:46 AM
I understand what your saying, but its still not working. i tried the code you cleaned up for me,and it didnt work. Then i tried alot of different things for about half an hour, and no difference.
CometWolf #7
Posted 27 February 2014 - 05:21 AM
The code he posted wasn't supposed to work
Part of your problem is that you've misplaced at least one "end". Here's the code with proper indentation, which'll hopefully make clear to you where they should be moved:
Bomb Bloke #8
Posted 27 February 2014 - 05:40 AM
To be clear, the indentation refers to the spacing - it's purely cosmestic, however it's a handy visual aid that makes it relatively easy to see what will execute when.

For example, once the "while" loop first starts, everything inside that loop gets indented to the right a bit further to make it clear that that's the code to be looped. Then when the first "if" statement is reached, the code in that gets indented further to indicate that's the code to run if the condition resolves as true. And so on.

This is the same section of code corrected to do something closer to what you're after. It's not perfect by any means, for now I'm just trying to get across the point as to where you went wrong and how indentation can be used to help you spot such mistakes:

Spoiler
while true do
	event, p1 = os.pullEvent()

	if event == "player" then
		local open = false            -- If this is still false...

		for i = 1, #players do
			if players[i] == p1 then
				openDoor()
				open = true   -- ... after completing this loop...
				break
			end
		end

		if not open then              -- ... then we'll do this:
			chat.say("You are not authorised to use this.")
		end
		
		-- And we'll do this whenever ANY player events are received:
		shell.run("ccGlasses")
		print("Door Attempted By: "..p1)
		sleep(5)
		term.clear()
	end
	
	if event == "redstone" then
		openDoor()
	end
		
	if event == "key" and p1 == keys.q then
		break
	end
end
Edited on 27 February 2014 - 04:44 AM
tacohaha #9
Posted 27 February 2014 - 09:32 PM
Wow. i didnt even look at your reply and i found out the problem on my own. im proud of myself. :D/> anyways thanks for helping