Jump to content




[Lua][Help]Rednet ID Help

help lua utility

  • You cannot reply to this topic
12 replies to this topic

#1 ChaddJackson12

  • Members
  • 264 posts

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.

#2 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

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!

#3 ChaddJackson12

  • Members
  • 264 posts

Posted 06 December 2012 - 01:25 PM

View PostDlcruz129, on 06 December 2012 - 01:20 PM, said:

When you read the line, it returns a string, or piece of text. Do something like this:
rednet.send(tonumber(Server),"msg")
Alright, thanks.

#4 ChunLing

  • Members
  • 2,027 posts

Posted 06 December 2012 - 08:22 PM

Why are you getting the computerID from a file instead of just using os.computerID()?

#5 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

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

#6 ChunLing

  • Members
  • 2,027 posts

Posted 07 December 2012 - 12:26 AM

Oh, that makes sense. Except that the IsServer boolean suggests that it could be a server.

#7 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

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

#8 ChaddJackson12

  • Members
  • 264 posts

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.

#9 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

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?

#10 ChaddJackson12

  • Members
  • 264 posts

Posted 07 December 2012 - 01:22 PM

View PostKaoS, on 07 December 2012 - 11:57 AM, said:

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

#11 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

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

#12 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 08 December 2012 - 02:52 AM

View PostKaoS, on 07 December 2012 - 11:57 AM, said:

why don't you just say if server~=nil then? that way it checks if 'server' is defined?
or just
if server then
.-.

#13 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 08 December 2012 - 02:55 AM

View PostPixelToast, on 08 December 2012 - 02:52 AM, said:

View PostKaoS, on 07 December 2012 - 11:57 AM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users