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

Table not being sent across Rednet

Started by Monkeymad358, 14 December 2014 - 08:52 PM
Monkeymad358 #1
Posted 14 December 2014 - 09:52 PM
Hello, I am trying to make a password protected program which will lock certain rooms off to people with different access levels. The access computers that ask users to input their username and password then asks one of the main servers in my base for a list of all users, to see whether or not the user is allowed in. I have tried everything I can to try and fix the issue, however this is my first time programming in lua and therefore I still don't understand a lot. Below is my code for the access computer and the main server.

Access Computer:

Spoiler

os.pullEvent = os.pullEventRaw

function topBar()
term.clear()
term.setCursorPos(1, 1)
print("MMOS Version 1.0")
term.setCursorPos(1, 3)
end

rednet.open("right")

while true do

rednet.send(3, "Send userList")

repeat
  receivedId, userListRednet = rednet.receive()
until receivedId == 3

f = fs.open("userListRednet", "r")
userList = textutils.unserialize(f.readAll())
f.close()

topBar()
print("Enter your username:")
name = read()
for i = 1, #userList do
  if name == userList[i][1] then
   userPassword = userList[i][2]
   userAccessLevel = userList[i][3]
   break
  end
end

topBar()
print("Enter your password:")
password = read("*")
if password == userPassword then
  topBar()
  textutils.slowPrint("Welcome "..name..". You have been granted Level "..userAccessLevel.." Access.")
  redstone.setOutput("back", true)
  term.setCursorPos(1, 6)
  textutils.slowPrint("The entrance will close in 16 seconds.")
  for i = 1, 15 do
   secondsLeft = 16 - i
   term.setCursorPos(1, 6)
   term.clearLine()
   print("The entrance will close in "..secondsLeft.." seconds.")
   sleep(1)
  end

  topBar()
  textutils.slowPrint("The entrance is now locked.")
  sleep(3)
  redstone.setOutput("back", false)
else
  topBar()
  print("Access Denied.")
  sleep(3)
end
end

Main Server:

Spoiler

os.pullEvent = os.pullEventRaw

while true do

userList = {{"name1", "password1", 1}, {"name2", "password2", 2}, {"Name3", "password3", 3}}

receivedId, receivedMessage = rednet.receive()
userListRednet = textutils.serialize(userList)
rednet.send(0, userListRednet)

end

Thank you :)/>
theoriginalbit #2
Posted 14 December 2014 - 10:47 PM
are you getting an error or unexpected results? if so, what is happening, more details into the problem help us better assist you
Bomb Bloke #3
Posted 14 December 2014 - 10:54 PM
repeat
  receivedId, userListRednet = rednet.receive()
until receivedId == 3

f = fs.open("userListRednet", "r")
userList = textutils.unserialize(f.readAll())
f.close()

… did you mean…?:

repeat
  receivedId, userListRednet = rednet.receive()
until receivedId == 3

userList = textutils.unserialize(userListRednet)
Monkeymad358 #4
Posted 15 December 2014 - 05:00 PM
are you getting an error or unexpected results? if so, what is happening, more details into the problem help us better assist you

I am getting no error, however when I run the two programs nothing happens. I have inserted print commands throughout both programs, and seen that they are both getting stuck in the repeat until loops. This happens when I run the main server first and then access computer, as well as when I run the access computer first and then the main server. I assume this is because they are both asking for data off each other, however I don't know why since I thought they would be asking for data at different times. I have also tried to remove the repeat until loops, however the two programs still don't work.

repeat
  receivedId, userListRednet = rednet.receive()
until receivedId == 3

f = fs.open("userListRednet", "r")
userList = textutils.unserialize(f.readAll())
f.close()

… did you mean…?:

repeat
  receivedId, userListRednet = rednet.receive()
until receivedId == 3

userList = textutils.unserialize(userListRednet)

I tried that and it seems to work since the program doesn't break, however the programs just freeze when they are waiting to receive data, so I don't know if it works or not yet.

I forgot to mention that I also tried to use rednet.broadcast() in case I had used the wrong id's, however that don't work either.
Edited on 15 December 2014 - 04:30 PM
Monkeymad358 #5
Posted 15 December 2014 - 05:40 PM
I found the reason the programs were not working. When the main server tried to send the access computer the data, the modem was turned on however the program didn't have the rednet.open() command to allow for data to be sent via rednet. I feel a little silly for missing such a simple thing, since I thought that turning on the modem also turned on ablity for the computer to use rednet, although I suppose I won't ever forget it again :P/> Thanks for everyone who tried to help anyway.