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

Hi I´m a noobie that is starting in programming. Could somebody check this program?

Started by VallField, 15 August 2014 - 06:47 PM
VallField #1
Posted 15 August 2014 - 08:47 PM
Hello. This is the program.

+


And when i run the program it give me this error.




Could somebody help me? Im a totally noobie in programing.
Dragon53535 #2
Posted 15 August 2014 - 09:42 PM
You started out fine, however you're creating a timer that fires an event when it reaches completion.
Spoiler

rs.setOutput("right",true) --# You already know this turns on the redstone
print("Buenos Dias") --# You already know this prints to the screen
local p = os.startTimer(3) --# This is a local variable, basically you''re setting 'p' to equal the value of os.startTimer(3) so that you can refer to it later.
while true do --# this will repeat everything below it until the end, represented by the end that you will see below it.
  local event, timerID = os.pullEvent("timer") --# This looks for an event to happen, and with the string "timer" its looking for a timer event only
  if timerID == p then --# compares the timer that it found and sees if its the same as the timer you started earlier
	rs.setOutput("right",false) --# turns off the redstone
	break --# gets you out of the while true do loop, if you didnt have this it would go right back up to the local event, timerID line
  end --# You need ends after every while loop, if statement, and basically most things, if you ever get an <eof> error, you have too many
end
local k = os.startTimer(480) --# starts a new timer with 480 seconds linked to the variable k. This happens AFTER the initial timer p happens earlier
while true do --# another while true do loop, this is an infinite loop which will keep going until you leave it using break
  local event, param1 = os.pullEvent("timer") --# another event, in this case param1 is the same as timerID, i just changed the name. You can set it to pizza and it will work
  if param1 == k then --# same as before. if you do change it to pizza, you have to change the param1 here to pizza as well :P/>/>/>/>/>/>
	os.reboot() --# restarts the computer
  end
end

Now there probably is a way to compact this code, however for the beginning it's quite fine, although i'm confused as to the purpose.

EDIT: Okay i overcomplicated it, an easier code, as Kouksi44 pointed out is that you're just waiting, so you just need a sleep()

rs.setOutput("right",true)
print("Buenos Dias")
sleep(3) --# just waits for 3 seconds
rs.setOutput("right",false)
sleep(480)
os.reboot()
Edited on 15 August 2014 - 07:59 PM
Kouksi44 #3
Posted 15 August 2014 - 09:47 PM
First of all the os.startTimer() function is not the one you need right There.

It looks like you want your program to wait for a spezific time .
In this case use os.sleep(time)

This way you avoid zwing this " if t=0 " stuff. But anyway, if you want to compare values you have to use == instead of = ;)/>
Just one "=" means you want to set a value and thats Not what you want to do right now :)/>
AdrenalineDM #4
Posted 15 August 2014 - 10:15 PM
Idk if this will help, but this is the code that I used for my simple password-protected door

http://pastebin.com/qnhaxbdt
VallField #5
Posted 15 August 2014 - 10:22 PM
Thank you very much guys!!! This have been very usefull!
Bomb Bloke #6
Posted 16 August 2014 - 03:42 AM
In regards to the original error, it's complaining about line three:

os.startTimer(3) = p

Here you're trying to start a timer, then replace whatever value the timer function returns (which isn't assigned to anything) with "p" (a variable set to nil). The Lua VM then goes cross eyed trying to figure out how that could make sense and throws an error at you.

You meant:

p = os.startTimer(3)