4 posts
Posted 20 May 2012 - 01:54 AM
Keep getting a rednet:337: string expected with this code
print("Welcome To Messenger")
rednet.open("right")
sleep(2)
print("And For How Long Will You Wait For Messages?(In Minutes)")
local time=read()*60
print("Enter Username")
local id=read()
sleep(2)
local event=rednet_message
print("Send A Message Or Receive One?(Type Send Or Receive)")
read()
if "read(send)" then
print("Enter Message")
text=read()
rednet.broadcast(message)
sleep(2)
end
if "read(Receive)" then
print("Reading Incoming Messages")
rednet.open("right")
while true do
event, id, text = os.pullEvent(rednet.broadcast)
if "event=rednet_message" then
print("id .. ">" .. text")
end
end
end
print("Send A Message Or Receive One?")
read()
Im really new so I was trying to make a rudimentary chat program with usernames and really basic messageing please help! also heres the file.
1604 posts
Posted 20 May 2012 - 02:42 AM
You should read some lua tutorial to learn how it works. I commented the code with some errors:
print("Welcome To Messenger")
rednet.open("right")
sleep(2)
print("And For How Long Will You Wait For Messages?(In Minutes)")
local time=read()*60 -- read() returns a string, you can't multiply strings. use tonumber("string") to convert strings to numbers.
print("Enter Username")
local id=read()
sleep(2)
local event=rednet_message -- this does nothing, rednet_message is nil (not defined)
print("Send A Message Or Receive One?(Type Send Or Receive)")
read() -- you need to store the return value of read() to use it, like: local input = read()
if "read(send)" then -- "read(send)" is a string, it will always evaluate to true. Using the above input, it should be: if input == "Send" then
print("Enter Message")
text=read()
rednet.broadcast(message) -- message is not defined, you used "text" to store the message, so use: rednet.broadcast(text)
sleep(2)
end
if "read(Receive)" then -- same as above, and you should use an elseif statement: elseif input == "Receive" then, and remove the above end so it doesn't cause an error
print("Reading Incoming Messages")
rednet.open("right") -- you already did this, not needed here
while true do
event, id, text = os.pullEvent(rednet.broadcast) -- you probably mean os.pullEvent("rednet_message"), rednet.broadcast is a function
if "event=rednet_message" then -- it should be: if event == "rednet_message", but is not needed since you used a filter above, so you only get "rednet_message" events
print("id .. ">" .. text") -- some extra quotes, should be: print(id..">"..text)
end
end
end
print("Send A Message Or Receive One?")
read() -- this shouldn't be here, it would wait for the user to write something and then exit the program
4 posts
Posted 20 May 2012 - 03:22 AM
thanks a bunch just one quick question, how would I loop it after the last if statement to let me send or recieve a message again?
1 posts
Posted 21 June 2012 - 08:16 PM
this might help even though as it is, it doesn't work
print("Welcome To Messenger")
rednet.open("right")
sleep(.5)
– print("And For How Long Will You Wait For Messages?(In Minutes)")
– local time = io.read()*60
print("Enter Username")
local id = io.read()
sleep(1)
local event=rednet_message
while true do
print("Send A Message Or Receive One?(Type send Or receive)")
local sr = io.read()
if sr = "send" then
print("Enter Message")
text = io.read()
rednet.broadcast(text)
sleep(.5)
end
if sr = "recieve" then
print("Reading Incoming Messages")
rednet.open("right")
local r m = rednet.receive()
print(r.. m..)
end
end