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

Functions are confusing...

Started by superlegostud, 03 August 2013 - 12:24 PM
superlegostud #1
Posted 03 August 2013 - 02:24 PM
OK, so I am working on a mail program for a FTB mindcrack server and I came across this error in my program only after I added the IM code into a function.

rednet:53: Expected number

Pastebin: http://pastebin.com/aUGn7Hyc

Variables (I will tell you what they mean to help make it easier to understand) :
  • choice, this allows the user to chose between IM and Email.
  • who, this is the ID of the person the user is messaging.
  • aid, this was something I was going to add later to only allow messages from who you are talking to.
  • smsg, this is the message being sent.
  • rmsg, this is the message being received.
I am trying to make it so it will keep on asking for a new message after receiving one. Don't bother with the email section i can do that on my own :D/> .

Thank-you for your time.
Lyqyd #2
Posted 03 August 2013 - 04:05 PM
Split into new topic.

You've declared `who` as local to your if block, so as soon as that block ends, the variable is no longer available. When you try to use it in your rednet.send call, it is nil, since it is outside of the if block. You should instead declare it as local before the if block (no value necessary, just `local who`) and then set the value inside the if block (remove the `local` keyword).
superlegostud #3
Posted 03 August 2013 - 04:12 PM
Oh. Thank-you for responding so quickly. :P/>

So if I remove the local will it be "fixed"?
LBPHacker #4
Posted 04 August 2013 - 04:11 AM
Yup, remove the local keyword, and - as mentioned above - put "local who" before the first print. That way "who" really will be local, the only change is that it is now in the main function (program) body, and everything in your program can access it, not only the if block.
superlegostud #5
Posted 06 August 2013 - 11:53 AM
OK. Thank-you. I thought that would work but I couldn't find my emulator at the time. One more question :P/> . How would I make a loop from

print("Enter message")
to

print(rmsg)

I want it to make a loop so that the users can rapid fire message each other without the need of reopening the program.


Thank-you for your time.



I figured it out never mind :D/> . Just used a while true do loop.