2 posts
Posted 09 May 2012 - 06:21 PM
Going into the lua interpreter and typing, rednet.open("right") works just fine as intended.
However, in my startup I have
write("What side is the modem on? ")
Side = read()
if (Side == "right") then
rednet.open("right")
end
Whenever I tell it the side is right and it tries to open it, it claims to be an invalid side…
When I tried doing
rednet.open(Side)
It claimed it wasn't a string.
Help please?
113 posts
Posted 09 May 2012 - 06:42 PM
Try not doing capital letters.
436 posts
Posted 09 May 2012 - 08:19 PM
tolower(Side) or Side = tolower(read()).
Those two will catch that problem for most things. Also, you can just do
side = tolower(read())
if side == "right" or "left" or "top" or "bottom" or "front" or "back" then
if peripheral.getType(side) == "modem" -- obviously only if you are using a modem and not cables.
rednet.open(side)
else
print("Not a modem, please type a different side")
end
else
print("Side: "" .. side .. " " : Side not valid, please try again")
end
This will open whatever side you want, provided it is a modem, and the program detects said modem.
And, for the "not a string" segment, I would need to see the actual code, but EmTeaKay is probably right. use lower case letters for variables. Typing hte same thign over and over can be a littel bti of a hasels and cause unwanted mistakes.
1604 posts
Posted 09 May 2012 - 08:33 PM
side = tolower(read())
if side == "right" or "left" or "top" or "bottom" or "front" or "back" then
if peripheral.getType(side) == "modem" -- obviously only if you are using a modem and not cables.
rednet.open(side)
else
print("Not a modem, please type a different side")
end
else
print("Side: "" .. side .. " " : Side not valid, please try again")
end
this:
if side == "right" or "left" or "top" or "bottom" or "front" or "back" then
should be:
if side == "right" or side == "left" or side == "top" or side == "bottom" or side == "front" or side == "back" then
Otherwise you are comparing if the variable side equals to the string "right" or the strings "left", "top", "bottom", "front" and "back" are not nil (wich will always return true), so you could input whatever you want and it would always say it's ok. Just saying cause I've seen this error a lot.
@TameCaveMan: is that the exact code you are using, cause it should work like that.
2447 posts
Posted 09 May 2012 - 08:51 PM
It's best just to tolower() the read() and just pipe it straight into the command - let the users make their own mistakes. Or you just detect what side the modem is on like so:
local side
for k, v in pairs(rs.getSides()) do
if peripheral.getType(v) == "modem" then
side = v
break
end
end
if not side then
error("No modem detected")
else
rednet.open(side)
end
39 posts
Posted 09 May 2012 - 11:06 PM
Try:
write("What side is the modem on? ")
side = read()
if side == "right" then
rednet.open("side")
end
2 posts
Posted 10 May 2012 - 05:41 PM
Thank you for the response, it was indeed a simple typo that I didn't see.. I was tired and completely missed it! XD