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

How to post a variable and string in a rednet.send

Started by brickw1994, 13 June 2012 - 10:57 PM
brickw1994 #1
Posted 14 June 2012 - 12:57 AM
Im trying to send a message with rednet.

the basic functionality is so it can be logged on a serverside computer.



Client sending script:

name = fs.exists("log1") -- asks if this file exists
rednet.send(87,..name.."Logged Off")

its supposed to send the info on what he did plus his "name"

so for example on the server log it would show

Server logger:

local time = os.time()
time = textutils.formatTime(time,true)
rednet.open("back")
id, m = rednet.receive()
print(" "..m.." On Computer: " ..id.." {"..time.."}")
sleep(1)
shell.run("log") ---( repeats this script again)--
MysticT #2
Posted 14 June 2012 - 01:23 AM
fs.exists just checks if a file exists, and it returns a boolean (true or false) not the file contents. To read the file, you have to open it with fs.open and the read from the file handle with file.read.
Example:

local file = fs.open("log1", "r")
if file then
  rednet.send(87, file.readAll())
  file.close()
end
And for the server, just don't use shell.run to repeat the program, it will crash the computer after some time. You have to use a loop:

rednet.open("back")
while true do
  local time = textutils.formatTime(os.time(), true)
  local id, m = rednet.receive()
  print(m, " On Computer: ", id, " {", time, "}")
  sleep(1)
end
brickw1994 #3
Posted 14 June 2012 - 02:27 AM
how do yo create a file without a disk, only using fs.
and,
how do you write a pre-entered variable into a file.write()

like this? because if it is it comes up attempt index nill


fs.makeDir("log1")
file = fs.open("log1","w")
name = read()
file.write(name)
file.close()
MysticT #4
Posted 14 June 2012 - 06:13 PM
how do yo create a file without a disk, only using fs.
and,
how do you write a pre-entered variable into a file.write()

like this? because if it is it comes up attempt index nill


fs.makeDir("log1")
file = fs.open("log1","w")
name = read()
file.write(name)
file.close()
fs.makeDir creates a directory/folder, and you can't open it like a file. Remove the fs.makeDir call, then delete that directory, and it should work. Also, you should always check if the file was opened:

local file = fs.open("path", "w")
if file then
  file.write("Some text here")
  file.close()
else
  print("Error opening file")
end