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

[Lua Question] Error message

Started by Jason92, 29 November 2016 - 03:29 AM
Jason92 #1
Posted 29 November 2016 - 04:29 AM
Hi guys current i making a simple program for enter key but an error occur in 3rd line.

Is there any problem with these coding?

function(input)
local event
If event == input then
local key = os.pullEvent()
key = keys.enter
end
end
Bomb Bloke #2
Posted 29 November 2016 - 06:19 AM
Lua is case sensitive; you meant "if" instead of "If".

Other than that, it's not obvious what you expect this script to actually do. If you have further problems, provide more details on what you're actually expecting.
Twijn #3
Posted 30 November 2016 - 01:02 AM
Assuming you're trying to test for a "key" event, and testing that the key expected is meant to be the enter key, here is what you could be doing wrong:
  1. You're defining a function, however you're not giving it a name. This will likely result in a error looking near to: 'name' expected.
  2. To define the function, just put the name between the 'function' and the '()'. For example, function test(input) … end can be run with test(input)
  3. The variable event is being defined to nil. You're then checking if input is equal to event (= nil), which is only true when you do not give it input at all.
  4. The variable key will be assigned to the event from os.pullEvent.
  5. The variable key is then overwritten to the value of keys.enter, which is 28. Therefore keys will be equal to 28 instead of the event from os.pullEvent.
How to fix these problems:
  1. If you're looking to only get the event which is inserted by input ,you can just instead use the line os.pullEvent(input), as os.pullEvent allows for one optional argument defining which events it will filter to you.
  2. Look at the page for os.pullEvent. In order to get the key which has been pressed for the event, "key" or "key_up", it will put the key as the first parameter, however event is technically the first so this would be second. You can use something like this, "local event,key = os.pullEvent(input)" so that it will assign the first parameter to "event" and the second to "key"
  3. Use the "key" variable you assigned above to test if key == keys.enter.