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

[Error] Rednet - string expected

Started by Calacene, 13 August 2012 - 05:28 PM
Calacene #1
Posted 13 August 2012 - 07:28 PM
Hi, I'm making a little server - client door lock program, for a little server I play on, but I'm getting this error - rednet:350: string expected - on the server computer.


Its probably something obvious, but I'm still missing it.. so


Client code-

while true do
local pwresult = "  "
local modem = "back"
term.clear()
term.setCursorPos(1,1)

print("Welcome to Cal Co.")
sleep(0.3)
write("Please enter your password: ")
local pw = tostring(read("*"))
term.clear()
term.setCursorPos(1,1)
sleep(0.1)
textutils.slowPrint(".........")
sleep(0.1)
rednet.open( modem)
rednet.send(5, pw)
sleep(0)
pwresult = rednet.receive(8)
term.clear()
term.setCursorPos(1,1)
if pwresult >= 1 then-- change this depending on priority of the door
print("Access granted, please continue.")
rs.setBundledOutput( "right", colors.white)
  sleep(2.3)
  rs.setBundledOutput( "right", 0)

else
print("Access denied, please try again.")
end
end



Server code-

while true do
local SentPass = "wrong"
local modem = "back"
sleep(0)
rednet.open(modem)
senderId, SentPass = rednet.receive()
local AllowedIds = { 6, 9}
local Passwords = { {name = "pass"}, {name2 = "pass2"}, {name3 = "pass3"}}
if senderId == AllowedIds[1] then
  sleep(0)
  if SentPass == Passwords[1][name] then
	rednet.send( SenderId, 1)

  elseif SentPass == Passwords[2][name2] then
	rednet.send( SenderId, 2)

  elseif SentPass == Passwords[3][name3] then
	rednet.send( SenderId, 3)
  else
	rednet.send( SenderId, 0)
  end
else
rednet.send( SenderId, 0)
end
end


It might be something to do with the tables, because I haven't used them before, and they kinda confuse me, except for the rednet part in the error, so idk.


Thank you,
ardera #2
Posted 13 August 2012 - 07:33 PM
Fixed Code:

while true do
local SentPass = "wrong"
local modem = "back"
sleep(0)
rednet.open(modem)
senderId, SentPass = rednet.receive()
local AllowedIds = { 6, 9}
local Passwords = { name = "pass", name2 = "pass2", name3 = "pass3"}
if senderId == AllowedIds[1] then
  sleep(0)
  if SentPass == Passwords.name then
		rednet.send( SenderId, "1")
  elseif SentPass == Passwords.name2 then
		rednet.send( SenderId, "2")
  elseif SentPass == Passwords.name3 then
		rednet.send( SenderId, "3")
  else
		rednet.send( SenderId, "0")
  end
else
rednet.send( SenderId, "0")
end
end

1. You can only send strings via rednet not numbers or tables or function or threads…
2. Quick Tutorial to tables :P/>/>:
if you do
table1={ var1="hello" }
then you can reach it under
table1.var1
. If you use
table1={ [1]="hello"}
then you can reach under
table1[1]
you can also use strings as index's (table1={["this is a string"]="string1"} reachable: table1["this is a string"]) its easy to work with tables, if you worked with tables some times.
Kolpa #3
Posted 13 August 2012 - 07:48 PM
take a look at this:
server: http://pastebin.com/DDniwcLY
client: http://pastebin.com/W3ppnLEH
Calacene #4
Posted 13 August 2012 - 08:48 PM
Fixed Code:

while true do
local SentPass = "wrong"
local modem = "back"
sleep(0)
rednet.open(modem)
senderId, SentPass = rednet.receive()
local AllowedIds = { 6, 9}
local Passwords = { name = "pass", name2 = "pass2", name3 = "pass3"}
if senderId == AllowedIds[1] then
  sleep(0)
  if SentPass == Passwords.name then
		rednet.send( SenderId, "1")
  elseif SentPass == Passwords.name2 then
		rednet.send( SenderId, "2")
  elseif SentPass == Passwords.name3 then
		rednet.send( SenderId, "3")
  else
		rednet.send( SenderId, "0")
  end
else
rednet.send( SenderId, "0")
end
end

1. You can only send strings via rednet not numbers or tables or function or threads…
2. Quick Tutorial to tables :P/>/>:
if you do
table1={ var1="hello" }
then you can reach it under
table1.var1
. If you use
table1={ [1]="hello"}
then you can reach under
table1[1]
you can also use strings as index's (table1={["this is a string"]="string1"} reachable: table1["this is a string"]) its easy to work with tables, if you worked with tables some times.

Thanks, that works great, no more errors.

However, for some reason its opening the door no matter what the inputted password is, any ideas? I'll keep looking.
Kolpa #5
Posted 13 August 2012 - 09:05 PM
Fixed Code:

while true do
local SentPass = "wrong"
local modem = "back"
sleep(0)
rednet.open(modem)
senderId, SentPass = rednet.receive()
local AllowedIds = { 6, 9}
local Passwords = { name = "pass", name2 = "pass2", name3 = "pass3"}
if senderId == AllowedIds[1] then
  sleep(0)
  if SentPass == Passwords.name then
		rednet.send( SenderId, "1")
  elseif SentPass == Passwords.name2 then
		rednet.send( SenderId, "2")
  elseif SentPass == Passwords.name3 then
		rednet.send( SenderId, "3")
  else
		rednet.send( SenderId, "0")
  end
else
rednet.send( SenderId, "0")
end
end

1. You can only send strings via rednet not numbers or tables or function or threads…
2. Quick Tutorial to tables :P/>/>:
if you do
table1={ var1="hello" }
then you can reach it under
table1.var1
. If you use
table1={ [1]="hello"}
then you can reach under
table1[1]
you can also use strings as index's (table1={["this is a string"]="string1"} reachable: table1["this is a string"]) its easy to work with tables, if you worked with tables some times.

Thanks, that works great, no more errors.

However, for some reason its opening the door no matter what the inputted password is, any ideas? I'll keep looking.
again:
ardera #6
Posted 13 August 2012 - 09:06 PM
Fixed Client Code:

while true do
local pwresult = "  "
local modem = "back"
term.clear()
term.setCursorPos(1,1)

print("Welcome to Cal Co.")
sleep(0.3)
write("Please enter your password: ")
local pw = tostring(read("*"))
term.clear()
term.setCursorPos(1,1)
sleep(0.1)
textutils.slowPrint(".........")
sleep(0.1)
rednet.open( modem)
rednet.send(5, pw)
sleep(0)
tonumber(pwresult) = rednet.receive(8)
term.clear()
term.setCursorPos(1,1)
if pwresult >= 1 then-- change this depending on priority of the door
print("Access granted, please continue.")
rs.setBundledOutput( "right", colors.white)
  sleep(2.3)
  rs.setBundledOutput( "right", 0)

else
print("Access denied, please try again.")
end
end

(Same Error as in Server Code)
ardera #7
Posted 13 August 2012 - 09:11 PM
@Kolpa: This is an other program: our Quest (<– don't know if I used the right word… Im not very good in English) is to fix their code, not to post links to other programs. If somebody posts "Errors in OS" then you can't post a link to another OS. (Another time: My English is bad.)
Kolpa #8
Posted 13 August 2012 - 09:12 PM
@Kolpa: This is an other program: our Quest (<– don't know if I used the right word… Im not very good in English) is to fix their code, not to post links to other programs. If somebody posts "Errors in OS" then you can't post a link to another OS. (Another time: My English is bad.)
its the same program and its my code :P/>/> im not telling him to copy it but to look at the code trying to fix it
Cranium #9
Posted 13 August 2012 - 09:13 PM
@Kolpa: This is an other program: our Quest (<– don't know if I used the right word… Im not very good in English) is to fix their code, not to post links to other programs. If somebody posts "Errors in OS" then you can't post a link to another OS. (Another time: My English is bad.)
The word you are looking for is "goal" Other than that, your english is pretty good.
Calacene #10
Posted 14 August 2012 - 07:22 AM
Thanks so much for the help, it works, now :P/>/>


I managed to finish off the debugging by myself, at least :D/>/>



Thank you,


*edit*

Actually… I'm having problems when adding new client computers.

the first one (id: 6) works, (there never was a 9, I just had it there, for some reason) But now, I've added computer #2 to the Allowed ids list, and the server refuses to respond to it, either that, or it isn't even getting sent.
I actually think it might be that second option, because I added code to make the server print when a password was accepted, denied, or any of that, and what computer, as well as the password used.

the client code is identical between the two pc's

Client code -


while true do
local pwresult = "  "
local modem = "back"
term.clear()
term.setCursorPos(1,1)
print("Welcome to Cal Co.")
sleep(0.3)
write("Please enter your password: ")
local pw = tostring(read("*"))
term.clear()
term.setCursorPos(1,1)
sleep(0.1)
textutils.slowPrint("............")
sleep(0.1)
rednet.open( modem)
rednet.send(5, pw)
sleep(0)
local senderId, pwresult = rednet.receive(8)
pwresult = tonumber(pwresult)
term.clear()
term.setCursorPos(1,1)
if pwresult >= 1 then-- change this depending on priority of the door
print("Access granted, please continue.")
rs.setBundledOutput( "right", colors.white)
  sleep(2.3)
  rs.setBundledOutput( "right", 0)
else
print("Access denied, please try again.")
sleep(1)
end
end

Server code -


print("This is the Cal. Co password server computer.")
while true do
local SentPass = "wrong"
local modem = "back"
sleep(0)
rednet.open(modem)
local senderId, SentPass = rednet.receive()
local AllowedIds = { 6, 2}
local Passwords = { name1 = "pass1", name1 = "pass2", name3 = "pass3"}
if senderId == AllowedIds[1] or senderId == AllowedIds[2] then
  sleep(0)
  if SentPass == Passwords.Calacene then
				rednet.send( SenderId, "1")
	print("Level 1 access granted: "..senderId.." - "..SentPass)
  elseif SentPass == Passwords.Shane then
				rednet.send( SenderId, "2")
	print("Level 2 access granted: "..senderId.." - "..SentPass)
  elseif SentPass == Passwords.Drake then
				rednet.send( SenderId, "3")
	print("Level 3 access granted: "..senderId.." - "..SentPass)
  else
				rednet.send( SenderId, "0")
	print("Access denied: "..senderId.." - "..SentPass)
  end
else
print("Computer denied: "..senderId.." - "..SentPass)
end
end

Also, quick question, but is it possible to make the computer check every string in the table, instead of doing the "if senderId == AllowedIds[1] or senderId == AllowedIds[2] then" thing?
Lyqyd #11
Posted 14 August 2012 - 03:01 PM

for k, v in ipairs(AllowedIDs) do
  if v == senderID then
    --code
  end
end
Calacene #12
Posted 14 August 2012 - 06:25 PM

for k, v in ipairs(AllowedIDs) do
  if v == senderID then
	--code
  end
end


Thanks, sorry, I'd forgotten that bit from the wiki.


I've been trying to get it to work, but still haven't had any luck, at one point, I did get it to receive the msg from pc #6, again, but it didn't recognize the id, and denied it.



print("This is the Cal. Co password server computer.")

while true do
local SentPass = "wrong"
local modem = "back"
sleep(0)
rednet.open(modem)
local senderId, SentPass = rednet.receive()
local AllowedIds = { 6, 2}
local Passwords = { name = "pass", name2 = "pass2", name3 = "pass3"}

for k,v in ipairs( AllowedIds )
  if v == senderID then
	  sleep(0)
	if SentPass == Passwords.name then
				rednet.send( SenderId, "1")
print("Level 1 access granted: "..senderId.." - "..SentPass)
	elseif SentPass == Passwords.name2 then
				rednet.send( SenderId, "2")
print("Level 2 access granted: "..senderId.." - "..SentPass)
	elseif SentPass == Passwords.name3 then
				rednet.send( SenderId, "3")
print("Level 3 access granted: "..senderId.." - "..SentPass)
	else
				rednet.send( SenderId, "0")
print("Access denied: "..senderId.." - "..SentPass)
	end
  else
  print("Computer denied: "..senderId.." - "..SentPass)
  end
  end
end

there might be something about the spacing, since I was playing around with it, trying to get it to work.

When I ran this last, it gave me the following error: bios:206: [string "ServerPass"]:13: 'do' expected
Lyqyd #13
Posted 15 August 2012 - 12:14 AM
Your for k, v line is missing its 'do'.
Calacene #14
Posted 15 August 2012 - 02:42 AM
Oh, wow, that was very obvious and I feel ashamed about that, I'd seen then's and all that, but hadn't noticed dos really, I guess.

no more errors, now, but the server is still outright denying computer #6
Noodle #15
Posted 15 August 2012 - 04:38 AM
no more errors, now, but the server is still outright denying computer #6
It shouldn't..
Is there are error, are you sending it from 6, using the right password?
Lyqyd #16
Posted 15 August 2012 - 05:59 AM
In the loop that goes through the allowed IDs, add this line for testing:


print(k.."= ("..v..")")

This should print out each key (1, 2) and each value, in parentheses. Please screenshot or copy exactly the output here. Spacing is important!
Calacene #17
Posted 15 August 2012 - 07:30 AM
Alright,


First of all, with the code, you meant like this, right?


for k,v in ipairs( AllowedIds ) do
  print(k.."= ("..v..")")
  if v == senderID then
	  sleep(0)

as for the ig example, the server is completely denying access to bot h of the computers, not even getting to where it checks passwords (all of which I blurred out, but were correct)





*edit*
Is the id received from rednet.receive a string or a number, because that might be the problem…. I tried



local tonumber(senderId), SentPass = rednet.receive()
--and
tonumber(senderId) = senderId

but both threw up errors at me.

the first one gave bios:206: [string "ServerPass"]:8: syntax error

the second gave me bios:206: [string "ServerPass"]:9: syntax error
Lyqyd #18
Posted 15 August 2012 - 02:50 PM
Alright,


First of all, with the code, you meant like this, right?


for k,v in ipairs( AllowedIds ) do
  print(k.."= ("..v..")")
  if v == senderID then
	  sleep(0)

as for the ig example, the server is completely denying access to bot h of the computers, not even getting to where it checks passwords (all of which I blurred out, but were correct)





*edit*
Is the id received from rednet.receive a string or a number, because that might be the problem…. I tried



local tonumber(senderId), SentPass = rednet.receive()
--and
tonumber(senderId) = senderId

but both threw up errors at me.

the first one gave bios:206: [string "ServerPass"]:8: syntax error

the second gave me bios:206: [string "ServerPass"]:9: syntax error

I believe it returns a number already. The syntax for using tonumber() is:


--set senderID to be itself converted to a number
senderID = tonumber(senderID)

I'll take a look at the code in more detail this afternoon.
Calacene #19
Posted 15 August 2012 - 05:42 PM
@Calacene: You shouldn't leave your passwords just printing themselves on a computer, unless you have lockette!

This is just single player atm, it will be heavily guarded once its on my server….. with a total of three close friends as the players.