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

[Lua][Error] rednet:347: positive number expected

Started by Kryptanyte, 14 November 2012 - 09:49 PM
Kryptanyte #1
Posted 14 November 2012 - 10:49 PM
Im creating a rednet communication system for my os however whenever i try and call a rednet.send([variable], [table]) where the variable is not in numerical form as in im getting the id to send to from


command, sendid, message = string.match(inputString, "(.-) (.-) (.*)")

where the input would be something like; /w 2 Hello There!

This is the only way I will be calling the id to send to like this.

cheers if anyone can help, I can post the entire chat code if needed (Be aware its unrefined as I have literally just made it to work atm.)
etopsirhc #2
Posted 14 November 2012 - 11:02 PM
the code really would help here :/
refined or not i have no clue whats going on with it w/o
remiX #3
Posted 14 November 2012 - 11:15 PM
Rather post the code so everyone can get a better understanding
jag #4
Posted 15 November 2012 - 01:37 AM
The error you are getting is from rednet, and the sort of error makes me think that something like this happens:
rednet.send(-1,"Message")
Orwell #5
Posted 15 November 2012 - 09:19 AM

command, sendid, message = string.match(inputString, "(.-) (.-) (.*)")

The variable 'sendid' now is a string, add this line under it to convert it to a number:

sendid = tonumber( sendid )

Then you can use it as the first parameter to rednet.send().
Kryptanyte #6
Posted 15 November 2012 - 02:04 PM
Messy and possibly hard to read but here it is.


local xMax, yMax = term.getSize()
yResult = yMax - 4
rednet.open("right")

i = 1
function drawSendBox(xStart, yStart)
term.setCursorPos(xStart+1, yStart)
newx = xMax - 2
o = 2
for i = 1, newx do
  term.write("_")
  term.setCursorPos(o, yStart + 3)  -- Writing the Horizonal Lines
  term.write("_")
  o = o + 1
  term.setCursorPos(o, yStart)
end

term.setCursorPos(xStart, yStart + 1)

for i = 1, 1 do
  term.write("|")
  term.setCursorPos(xMax, yStart + 1)
  term.write("|")
  term.setCursorPos(xStart, yStart + 2)
  term.write("|")
  term.setCursorPos(xMax, yStart + 2)  -- Writing the Vertical lines (NEED TO FIX FOR STATEMENT PRINTING!)
  term.write("|")
  term.setCursorPos(xStart, yStart + 3)
  term.write("|")
  term.setCursorPos(xMax, yStart + 3)
  term.write("|")
end
term.setCursorPos(xStart + 2, yStart + 2)

end
cl.clear()
term.setCursorPos(1,1)
while true do
event, id, message = os.pullEvent()
local curline = 0
if event == "rednet_message" then

local table = textutils.unserialize(message)
  
  if table[1] == "/w" then
  
  
   print("[Whisper] from "..table[2]..": "..table[3])
  
  
  else
  
   cl.clear()
   print("Error")
   break
  end
  
elseif event == "key" then
if id == 28 then
  
  drawSendBox(1, yResult)
  
  inputString = read()
  Myid = os.getComputerID()
  command, sendid, message = string.match(inputString, "(.-) (.-) (.*)")
  
  local table = {command, sendid, message}
  local sTable = textutils.serialize(table)
  
  rednet.send(sid, sTable)
  
  term.setCursorPos(1, yResult)
  term.clearLine()
  term.setCursorPos(1, yResult + 1)
  term.clearLine()
  term.setCursorPos(1, yResult + 2)
  term.clearLine()
  term.setCursorPos(1, yResult + 3)
  term.clearLine()
  
else


end

else

end

end



Feel free to fix anything if you can be bothered.

By the way, if you see a cl.clear() thats just a custom api using term.clear() term.setCursorPos(1,1) nothing special
Orwell #7
Posted 15 November 2012 - 02:17 PM
I already gave you the solution, but I'll do it again.

command, sendid, message = string.match(inputString, "(.-) (.-) (.*)")
After this line, the variable 'sendid' is a string. But when you use it in rednet.send() it has to be a number. So convert it to a number first by adding this right under that line:

sendid = tonumber( sendid )

Also, you declare 'sendid', but when you use the rednet.send function, you use 'sid', which isn't declared anywhere:

rednet.send(sid, sTable)
so make this:

rednet.send(sendid, sTable)

Change those two lines and that error should be fixed.
Kryptanyte #8
Posted 15 November 2012 - 02:33 PM
Yeah I saw you did that and put it into the code already but I decided to post the code for those who wanted to see it. Thanks though. Alse the sid is declared but I took the line out because it was sid = tonumber ( sendid ) so I left it out for those who wanted to have a go for themselves. Now to put in the rest of the commands =/
Orwell #9
Posted 15 November 2012 - 03:02 PM
Ah, ok, a bit strange to post non-working code, but I think I see your point. Glad I could help. :P/>/>
Kryptanyte #10
Posted 15 November 2012 - 03:26 PM
Just asking if you could help me again, theres an issue im having with trying to print messages being sent by the terminal to the same one that is sending.


local xMax, yMax = term.getSize()
yResult = yMax - 4
rednet.open("right")

i = 1
function drawSendBox(xStart, yStart)
 term.setCursorPos(xStart+1, yStart)
 newx = xMax - 2
 o = 2
 for i = 1, newx do
  term.write("_")
  term.setCursorPos(o, yStart + 3)  -- Writing the Horizonal Lines
  term.write("_")
  o = o + 1
  term.setCursorPos(o, yStart)
 end

 term.setCursorPos(xStart, yStart + 1)

 for i = 1, 1 do
  term.write("|")
  term.setCursorPos(xMax, yStart + 1)
  term.write("|")
  term.setCursorPos(xStart, yStart + 2)
  term.write("|")
  term.setCursorPos(xMax, yStart + 2)  -- Writing the Vertical lines (NEED TO FIX IF STATEMENT PRINTING!)
  term.write("|")
  term.setCursorPos(xStart, yStart + 3)
  term.write("|")
  term.setCursorPos(xMax, yStart + 3)
  term.write("|")
 end
 term.setCursorPos(xStart + 2, yStart + 2)
 
end
cl.clear()
term.setCursorPos(1,1)
while true do
event, id, message = os.pullEvent()
local curline = 0
if event == "rednet_message" then
 
 local table = textutils.unserialize(message)
  
  if table[1] == "/w" then
   
   
   print("[Whisper] from "..table[2]..": "..table[3])
   curline = curline + 1
   
   
  else
  
   cl.clear()
   print("Error")
   break
  end
  
elseif event == "key" then
 if id == 28 then
  
  drawSendBox(1, yResult)
  
  inputString = read()
  Myid = os.getComputerID()
  command, sendid, message = string.match(inputString, "(.-) (.-) (.*)")
  
  if command == "/w" then
  term.setCursorPos(1, curline)
  term.write("[Whisper] to "..sendid..": "..message)
  curline = curline + 1
  
  
  local table = {command, sendid, message, 2}
  
  term.setCursorPos(1, curline)
  
  local sTable = textutils.serialize(table)
  local sid = tonumber(sendid)
  
  rednet.send(sid, sTable)
  term.setCursorPos(1, yResult)
  term.clearLine()
  term.setCursorPos(1, yResult + 1)
  term.clearLine()
  term.setCursorPos(1, yResult + 2)
  term.clearLine()
  term.setCursorPos(1, yResult + 3)
  term.clearLine()
  term.setCursorPos(1, curline)
  end
  
 else
 
 
 end
 
else

end

end


I need to put in an if statement that checks the command given by the user to send different types of messages however it will not work wherever i put it after the input.

Any help is appreciated including bug reports (Also I know the sending my id has a bug in it. This might not be the right half of the code as Im fixing bugs as I see them on each file and not really doing both)
Orwell #11
Posted 15 November 2012 - 03:33 PM
Could you please elaborate on what the problem is? All I know is that you're having 'issues' with printing something. :P/>/> Do you get an error? Which one? What exactly do you expect to happen and what actually happens?
Kryptanyte #12
Posted 15 November 2012 - 04:08 PM
Sorry, I will explain a little bit more.

Basically I want it to print [Whisper] to [id]: [message]
when sending a message to someone. This is supposed to be done by;


if command == "/w" then
term.setCursorPos(1, curline)
print("[Whisper] to "..sendid..": "..message)
curline = curline + 1

Well it should be done like that so I can add more commands like talking to all computers within range or talking to all computers within as far as my CCServers go. etc etc
However when I try this code it doesnt print to the terminal its sending from. It only prints on the 2nd terminal that is receiving.

Just tell me if you need more info