I am starting my own email programs for a server with friends and have started by giving them an easy way to change what side the modem is attached to (the don't know Lua).
I started with this: http://pastebin.com/4gsvLThm (The pastebin link is better because the colour is correct)
Spoiler
function clearS()
term.setCursorPos(1,1)
term.clear()
end
function writeToFile(data)
h = fs.open("EmailData/ModemData", "w")
h.write(data)
h.close()
end
if fs.isDir("EmailData") == false then --Make sure the directory exists. I want the file in a directory so that the files won't show up when the user displays the list of programs on the PC.
fs.makeDir("EmailData")
end
clearS()
h = fs.open("EmailData/ModemData", "r")
modemSide = h.readAll() -- Find out what the side is before changes. This is where the error is.
h.close()
while true do --Loop so that they are asked until they provide a valid answer.
if modemSide == "left" or modemSide == "right" or modemSide == "top" or modemSide == "bottom" or modemSide == "back" or modemSide == "front" then
print("The modem is currently set to be on the " .. modemSide .. " of the computer.")
print("Would you like to change this?")
print("'Y' or 'N'? ")
event, param1 = os.pullEvent("key")
if param1 == 109 then
clearS()
print("The modem side will not be changed.")
sleep(2)
clearS()
break
elseif param1 == 121 then
clearS()
while true do --Loop so that they are asked until they provide a valid answer.
print("What side should the monitor be changed to?")
print("Please select a number:")
print("[1] Left")
print("[2] Right")
print("[3] Top")
print("[4] Bottom")
print("[5] Front")
print("[6] Back")
print("[0] Cancel")
local event, param2 = os.pullEvent("key") --Get the user input...
if param2 == 49 then
s = "left"
break
elseif param2 == 50 then --And compare it to the ASCII codes for those keys.
s = "right"
break
elseif param2 == 51 then
s = "top"
break
elseif param2 == 52 then
s = "bottom"
break
elseif param2 == 53 then
s = "front"
break
elseif param2 == 54 then
s = "back"
clearS()
break
elseif param2 == 48 then
clearS()
print("Cancelling...")
sleep(1.5)
else
clearS()
print("That is not a valid selection...")
sleep(1.5)
end
end
clearS()
writeToFile(s) -- Set the side to what the user wants.
print("You have sucessfully changed the side of the modem to: " .. s)
sleep(2)
break
else
clearS()
print("That's not a valid answer...")
sleep(1.5)
clearS()
end
elseif modemSide == nil then
writeToFile("right") -- Set the default side.
end
end
The error I get is:
FILENAME:19: attempt to index ? (a nil value)
I've seen this error many a time before, but I just can't work out what's going wrong. It seems that 'h.readAll()' is returning nil, and I can't work out why. I've done this before and not had trouble, and tried using 'h.readLine()' too.
This is all correct according to the wiki, but maybe it's just that I'm out of practice in Lua.
(and yes I know it could be neater, but I just want to get it working for now!)
Thanks in advance,
Ross.