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

Problem with rednet sending

Started by Kadecamz, 13 October 2012 - 04:10 PM
Kadecamz #1
Posted 13 October 2012 - 06:10 PM

rednet.open("back")
term.clear()
term.setCursorPos(9,2)
print("Would you like to launch a missile?")
term.setCursorPos(23,9)
print("Y/N")
event, p1 = os.pullEvent("key")
if p1 == 21 then
term.setCursorPos(19,10)
write("Printing Codes")
sleep(1)
write(".")
sleep(1)
write(".")
sleep(1)
write(".")
lcode = tostring( math.random(100000,999999) )
rednet.send(3,lcode) --attempt to index ? (a nil value)
printer.write(lcode)
printer.setPageTitle("LAUNCH CODE")
printer.endPage()
term.setCursorPos(19,10)
term.clearLine()
term.setCursorPos(19,10)
print("Completed!")
sleep(2)
rednet.broadcast(lcode)
shell.run("startup")
elseif p1 == 49 then
term.setCursorPos(19,18)
print("Aborted!")
sleep(3)
shell.run("startup")
end
Please tell what I did wrong with it
Also, are you able to make the computer text be colored in the console or do you have to use the config?
Lettuce #2
Posted 13 October 2012 - 06:31 PM
You forgot to put parenthesis after rednet.send() on the line above it.
Kadecamz #3
Posted 13 October 2012 - 06:39 PM
I made it rednet.send(2,lcode) and now its erroring me whenever the program reaches that line of code.


The error: rednet:350:string expected
Lyqyd #4
Posted 13 October 2012 - 06:55 PM
That would be because you never declare 'lcode'. Is this the whole code you're using, or only part of it?
Kadecamz #5
Posted 13 October 2012 - 07:18 PM
I did!

lcode is whatever the math.random generates
sjele #6
Posted 13 October 2012 - 07:20 PM
look on the line above lcode = math.random(100000, 99999999) and you will notice that you have a rednet.send() witch is missing the (), the id, and the msg
Lettuce #7
Posted 13 October 2012 - 07:25 PM
You need to declare lcode before trying to call it. Just reverse the order. i.e. putting math.random() before rednet.send().

*edit: Lyqyd, you've been :)/>/> 'd
Lyqyd #8
Posted 13 October 2012 - 07:26 PM
look on the line above lcode = math.random(100000, 99999999) and you will notice that you have a rednet.send() witch is missing the (), the id, and the msg

Look a few posts up, he said he changed that.

I did!

lcode is whatever the math.random generates

So how can you rednet.send that out before the line that generates it?
Kadecamz #9
Posted 13 October 2012 - 07:50 PM
I fixed that too, but still getting the error rednet:350:string expected

current code

rednet.open("back")
term.clear()
term.setCursorPos(9,2)
print("Would you like to launch a missile?")
term.setCursorPos(23,9)
print("Y/N")
event, p1 = os.pullEvent("key")
if p1 == 21 then
term.setCursorPos(19,10)
write("Printing Codes")
sleep(1)
write(".")
sleep(1)
write(".")
sleep(1)
write(".")
lcode = math.random(100000,999999)
rednet.send(3,lcode)
printer.write(lcode)
printer.setPageTitle("LAUNCH CODE")
printer.endPage()
term.setCursorPos(19,10)
term.clearLine()
term.setCursorPos(19,10)
print("Completed!")
sleep(2)
rednet.broadcast(lcode)
shell.run("startup")
elseif p1 == 49 then
term.setCursorPos(19,18)
print("Aborted!")
sleep(3)
shell.run("startup")
end
Orwell #10
Posted 13 October 2012 - 07:53 PM
I fixed that too, but still getting the error rednet:350:string expected

current code

rednet.open("back")
term.clear()
term.setCursorPos(9,2)
print("Would you like to launch a missile?")
term.setCursorPos(23,9)
print("Y/N")
event, p1 = os.pullEvent("key")
if p1 == 21 then
term.setCursorPos(19,10)
write("Printing Codes")
sleep(1)
write(".")
sleep(1)
write(".")
sleep(1)
write(".")
lcode = math.random(100000,999999)
rednet.send(3,lcode)
printer.write(lcode)
printer.setPageTitle("LAUNCH CODE")
printer.endPage()
term.setCursorPos(19,10)
term.clearLine()
term.setCursorPos(19,10)
print("Completed!")
sleep(2)
rednet.broadcast(lcode)
shell.run("startup")
elseif p1 == 49 then
term.setCursorPos(19,18)
print("Aborted!")
sleep(3)
shell.run("startup")
end

Do:

lcode = tostring( math.random(100000,999999) )
Kadecamz #11
Posted 13 October 2012 - 08:06 PM
Now I'm getting a problem on line 19.

It says attempt to index ? a nil value
Lyqyd #12
Posted 13 October 2012 - 08:20 PM
That'd be because you never declared 'printer'. It's like monitors, you have to peripheral.wrap() the side to that variable before you can use it.
Kadecamz #13
Posted 13 October 2012 - 08:39 PM
Yay!
Its working now.

But I have one more question, can you erase printed pages?
and how do you randomly generate a letter?
cant_delete_account #14
Posted 13 October 2012 - 08:48 PM
Yay!
Its working now.

But I have one more question, can you erase printed pages?
and how do you randomly generate a letter?
Make a table of the alphabet like this:

local alphabet = {"a","b","c","etc"}
Then use this code to print a random letter out of it:

print(alphabet[math.random(1,#alphabet)])
So final code:

local alphabet = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}
print(alphabet[math.random(1,#alphabet)])
And the alphabet does not have to be in order.
Kadecamz #15
Posted 13 October 2012 - 08:59 PM
Wait, but how do I do a mix of letters AND numbers, and then put it into a string to print?
Lettuce #16
Posted 13 October 2012 - 10:15 PM
Numbers can also be entered into tables.

table = {"1","2","3","4","5","a","b","c","d","e"}
Works just fine. I made the numbers strings, but they can be plain numbers too. Like {1,2,3}
Kadecamz #17
Posted 14 October 2012 - 12:22 AM
woops, rather dumb I didn't think about that.
Kadecamz #18
Posted 14 October 2012 - 01:01 AM
uhg.
guys, how do I get it to randomize six characters?
i dont want the codes being a single letter.
Lettuce #19
Posted 14 October 2012 - 01:06 AM
Maybe a for loop?


code = {}
for i = 1,6 do
code[i] = math.random(0,9)
end

Didn't test it, but it should work.

*edit: Upon seeing Fatal_Exception's post, I know how to give a table for that. Depending on what you need.

char = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"}
for i = 1,6 do
code[i] = char[math.random(1,#char)]
end

Again, Untested, but will work, unless you want to display it, in which case, listen to F_E below.

By the way, out of curiosity, is this for generating a code for your missiles, or cracking one?
Fatal_Exception #20
Posted 14 October 2012 - 01:07 AM
Repeat the process multiple times (in a for loop) and concatenate your result to the previous.

local alphabet = { (a table of characters here) }
local str = ""
for i = 1, 6 do
  str = str .. alphabet[math.random(1,#alphabet)]
end