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

[Lua][Help]Rednet ID Help

Started by ChaddJackson12, 06 December 2012 - 12:12 PM
ChaddJackson12 #1
Posted 06 December 2012 - 01:12 PM
I would like to know how to read a computer ID from a file, and use it in rednet.send(). I haven't been able to find a way to do it. Here is what I have tried.


if fs.exists("ServerID") then
  f = fs.open("ServerID", "r")
  Server = f.readLine()
  f.close()
  IsServer = true
else
  IsServer = false
end

I tried this out and it returned the error: Rednet:[347]: positive number expected. This is because I wanted to use this to send stuff to the server computer by using "rednet.send(Server, "Stuff")". I'm pretty sure that f.readLine() returns a string rather than a number, and so that would be the problem. Is there a way I could get the number out of the string? Please help, thanks in advance.
Dlcruz129 #2
Posted 06 December 2012 - 01:20 PM
When you read the line, it returns a string, or piece of text. Do something like this:
rednet.send(tonumber(Server),"msg")

EDIT: 300th post!
ChaddJackson12 #3
Posted 06 December 2012 - 01:25 PM
When you read the line, it returns a string, or piece of text. Do something like this:
rednet.send(tonumber(Server),"msg")
Alright, thanks.
ChunLing #4
Posted 06 December 2012 - 08:22 PM
Why are you getting the computerID from a file instead of just using os.computerID()?
KaoS #5
Posted 07 December 2012 - 12:03 AM
this code isn't run on the server, he is storing another computer's ID in a file so the program can recall it
ChunLing #6
Posted 07 December 2012 - 12:26 AM
Oh, that makes sense. Except that the IsServer boolean suggests that it could be a server.
KaoS #7
Posted 07 December 2012 - 01:01 AM
I think it is just to say that a server exists or is defined… which makes no sense lol
ChaddJackson12 #8
Posted 07 December 2012 - 11:51 AM
I am planning on making something that uses a server computer, but I don't want to have to have people who might use it edit the code… So I would have them type it in using an interface, and it stores it…

I ran into the problem with "Positive number expected". That is why I came here.

Also, the IsServer is so it can define Server, without running into an error before the user is able to define it using the interface. If IsServer is false, it wouldn't use any rednet commands.
KaoS #9
Posted 07 December 2012 - 11:57 AM
why don't you just say if server~=nil then? that way it checks if 'server' is defined?
ChaddJackson12 #10
Posted 07 December 2012 - 01:22 PM
why don't you just say if server~=nil then? that way it checks if 'server' is defined?
Well.. I didn't know about that, but either way works just as well :P/>
KaoS #11
Posted 08 December 2012 - 02:44 AM
It is quite useful to be able to check if a variable is defined, when you make a function you should not let it run if important variables are missing, it makes catching errors very easy
PixelToast #12
Posted 08 December 2012 - 02:52 AM
why don't you just say if server~=nil then? that way it checks if 'server' is defined?
or just
if server then
.-.
KaoS #13
Posted 08 December 2012 - 02:55 AM
why don't you just say if server~=nil then? that way it checks if 'server' is defined?
or just
if server then
.-.

that works perfectly too. just note that if server==false then it will also not execute, in some cases that can cause issues