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

[File.Error?] [lua] [coding] [table...] help?

Started by Goof, 24 November 2012 - 09:22 AM
Goof #1
Posted 24 November 2012 - 10:22 AM
Hey…

ehm.. i am making a server for passwords… (when a computer client asks for password, it sends the input to this server, and receives "Correct" or "False") but it seems like i got an error:
MikkAPI:414: attempt to call nil

Code?
its right here:
Spoileronly the server code:
Spoiler




function PassServer()
local oldDog = os.pullEvent
os.pullEvent = os.pullEventRaw
local runningP = true
  
Passwords = {}
IdS = {}

if not fs.exists("PdsWaid") then
fileCheck = fs.open("PdsWaid", "w")
for i, k in ipairs(Passwords) do
fileCheck.writeLine(Passwords[k])
end
fileCheck.close()
elseif fs.exists("PdsWaid") then
local fileRead = fs.open("PdsWaid", "r") -- here is the error LINE... i dont understand why?
local lines1 = fileRead.readLine()
repeat
table.insert(Passwords, lines1)
lines1 = fileRead.readLine()
until lines1 == nil

fileRead.close()
end

if not fs.exists("PdsPaid") then
fileChecks = fs.open("PdsPaid", "w")
for h, g in ipairs(IdS) do
fileChecks.writeLine(IdS[k])
end
fileChecks.close()
else
fileReads = fs.open("PsdsPaid", "r")
lines2 = fileReads.readLine()
repeat
table.insert(IdS, lines2)
lines2 = fileReads.readLine()
until lines2 == nil
fileReads.close()
end



function checkInput()
while running do
event, key = os.pullEvent("key")
if event == "key" then
if key == keys.Q then
running = false
end
end
end
end

function Server()
while runningServer do
id, msg = rednet.receive()
for i, v in ipairs(IdS) do
if id == IdS[v] then
for k, f in ipairs(Passwords) do
if msg == Passwords[f] then
rednet.send(id, "ValidPassword")
end
end
end
end
end
end


function LoadServer()
clear(1,1)
TSP("Loading Password Server v. 2.3")
sleep(1)
TSP("Loaded... Ready to Go...")
clear(1,10)
TSP("Type "Q" to exit")
sleep(2)
for i = 1, 9 do
term.setCursorPos(1, i)
term.clearLine()
end
parallel.waitForAny(Server, checkInput)
end

while runningP do
clear(1,1)
TSP("Welcome.....")
p("-----------------")
sleep(2)
TSP("Loading components...")
p("-----------------")
sleep(3)
TSP("Done!")
sleep(0.5)
clear(1,1)
TSP("Enter Password to boot server...")
sleep(1)
TSW("Password > ")
PassInput = read("#")
if PassInput == "ServerBootAccess_Key" then
clear(1,1)
runningP = false
LoadServer()
else
clear(1,1)
TSP("Wrong password ?")
sleep(4)
os.shutdown()
end
end
os.pullEvent = oldDog
end


Full code[huge(its a API)]:
Spoiler




function TSW( text )
textutils.slowPrint(text)
end

function TSP( text )
textutils.slowPrint(text)
end

function p( text )
print( text )
end

function w( text )
write( text )
end

function printCentered( PosY, text )
local x, y = term.getSize()
term.setCursorPos(x, PosY)
p(text)
end

function clear(x,a)
term.clear()
term.setCursorPos(x,a)
end

function goForward()
while not turtle.forward() do
turtle.dig()
sleep(1)
end
end

function goBack()
while not turtle.back() do
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.attack()
turtle.turnRight()
turtle.turnRight()
sleep(1)
end
end

function goUp()
while not turtle.up() do
turtle.digUp()
sleep(1)
end
end

function goDown()
while not turtle.down() do
turtle.digDown()
sleep(1)
end
end

function moveUp(x1)
BootSystematics_MikkAPIAPIS("goUp")
FuelLevel()
p("Starting API...")
for a3 = 1, x1 do
MikkTurtleAPI.goUp()
end
end

function moveDown(x2)
BootSystematics_MikkAPIAPIS("goDown")
FuelLevel()
p("Starting API...")
for a3 = 1, x2 do
MikkTurtleAPI.goDown()
end
end

function moveForward(x3)
BootSystematics_MikkAPIAPIS("goForward")
FuelLevel()
p("Starting API...")
for a3 = 1, x3 do
MikkTurtleAPI.goForward()
end
end

function turn(Bolean)
if Bolean == right or Bolean == "right" or Bolean == Right or Bolean == "Right" then
while not turtle.turnRight() do
sleep(1)
end
elseif Bolean == left or Bolean == "left" or Bolean == Left or Bolean == "Left" then
while not turtle.turnLeft() do
sleep(1)
end
end
end

function FuelLevel()
while turtle.getFuelLevel() < 100 do
clear(1,1)
p("I need fuel in slot number "1"")
turtle.select(1)
while not turtle.refuel() do
clear(1,1)
p("I need fuel.. put fuel in slot "1"")
end
end
end

--[Calling In-Game-Action IGA]--

function BootSystematics_MikkAPIAPIS( ApiChoose )
os.loadAPI("MikkTurtleAPI")
os.loadAPI("MikkConsoleAPI")
os.loadAPI("turtle")
clear(1,1)
printCentered( 7, "The Following API, has been entered: ""..ApiChoose..""")
sleep(0.7)
printCentered( 1, "Please Wait, while booting ""..ApiChoose..""")
sleep(2)
clear(1,1)
printCentered( 3, "Starting ""..ApiChoose..""")
end

--[In-Game Functions...]--

function doWall( high, length, SlotNum )

function placeDown()
while not turtle.placeDown() do
if SlotNum < 16 then
turtle.select(SlotNum+1)
SlotNum = SlotNum + 1
else
turtle.select(1)
SlotNum = 1
end
clear(1,1)
turtle.digDown()
sleep(1)
end
end

function place()
while not turtle.place() do
if SlotNum < 16 then
turtle.select(SlotNum+1)
else
turtle.select(1)
end
clear(1,1)
turtle.dig()
sleep(1)
end
end

function placeUp()
while not turtle.placeUp() do
if SlotNum < 16 then
turtle.select(SlotNum+1)
else
turtle.select(1)
end
clear(1,1)
turtle.digUp()
sleep(1)
end
end

function NextRow()
goForward()
for a0 = 1, high do
goDown()
end
end

function Build()
FuelLevel()
p("Starting API...")
for a1 = 1, length do
for a2 = 1, high do
goUp()
placeDown()
end
NextRow()
end
end

BootSystematics_MikkAPIAPIS( "doWall" )
turtle.select(SlotNum)
Build()

end

function doQuarry()
x = 1
xx = 1
xxx = 1
function NextRow1()

turtle.turnRight()
goForward()
turtle.turnLeft()


end

function NextLvl1()
if xx == 0 then
turtle.turnLeft()
for wid = 1, widTT2 - 1 do
goForward()
end
turtle.turnRight()
goForward()
xx = 1
else
turtle.turnLeft()
for wid = 1, widTT2 - 1 do
goForward()
end
turtle.turnRight()
goForward()
for ad = 1, heiTT2 - 1 do
goDown()
end
xx = 1
end
end

function NextRow2()
if x == 1 then
turtle.turnRight()
goForward()
turtle.turnRight()
x = 0
else
turtle.turnLeft()
goForward()
turtle.turnLeft()
x = 1
end
end

function NextLvl2()
if x == 1 then
turtle.turnLeft()
for wid0 = 1, widTT1 - 1 do
goForward()
end
turtle.turnRight()
goDown()
for i = 1, lenTT1 - 1 do
goBack()
end
x = 1
else
turtle.turnRight()
for wid0 = 1, widTT1 - 1 do
goForward()
end
turtle.turnRight()
goDown()
x = 1
end
end

	--[verticalised tunnel/hole]--

function QuarryVert1()

for hei2 = 1, heiTT1 do
for wid2 = 1, widTT1 do
for len2 = 1, lenTT1 do
turtle.digDown()
local lenv = tonumber(len2)
if lenv < lenTT1 then
goForward()
end
sleep(0.5)
end
local widv = tonumber(wid2)
if widv < widTT1 then
NextRow2()
end
end
NextLvl2()
end

for heiUp = 1, heiTT1 do
goUp()
end
end

	--[horisontial tunnel]--

function QuarryHori1()

for len1 = 1, lenTT2 do

for wid1 = 1, widTT2 do

for hei1 = 1, heiTT2 do
turtle.dig()
local heic = tonumber(hei1)

if heic < heiTT2 then
if xx == 1 then
goUp()
else
goDown()
end
end
end
local widc = tonumber(wid1)

if widc < widTT2 then

NextRow1()
if xx == 1 then
xx = 0
else
xx = 1
end
end
end
NextLvl1()
end

for heiBack = 1, lenTT2 do
goBack()
end

end

function QuarryVert()

clear(1,1)
w("Width? > ")
WiTe1 = read()
widTT1 = tonumber(WiTe1)
clear(1,1)
w("Length? > ")
LeTe1 = read()
lenTT1 = tonumber(LeTe1)
clear(1,1)
w("size? (how many rows it will dig down) > ")
HeTe1 = read()
heiTT1 = tonumber(HeTe1)
FuelLevel()
QuarryVert1()


end

function QuarryHori()

clear(1,1)
w("Width? > ")
WiTe2 = read()
widTT2 = tonumber(WiTe2)
clear(1,1)
w("Height? > ")
HeTe2 = read()
heiTT2 = tonumber(HeTe2)
clear(1,1)
w("Length? (the length of the tunnel) > ")
LeTe2 = read()
lenTT2 = tonumber(LeTe2)
FuelLevel()
QuarryHori1()

end

BootSystematics_MikkAPIAPIS("doQuarry")
verHor = true
while verHor do
clear(1,1)
p("Do you want the quarry, Vertical or Horisontial? Vertical is downwards, Horisontial is straight ahead...")
w(" Answer >>---> ")
VH1 = read()
if VH1 == "Vertical" or VH1 == "vertical" or VH1 == Vertical or VH1 == vertical then
verHor = false
QuarryVert()
elseif VH1 == "Horisontial" or VH1 == "horisontial" or VH1 == Horisontial or VH1 == horisontial then
verHor = false
QuarryHori()
else
verHor = true
clear(1,1)
p("Your answer does not match to Horisontial or Vertical ... Try again")
end
end
end

function PassServer()
local oldDog = os.pullEvent
os.pullEvent = os.pullEventRaw
local runningP = true
  
Passwords = {}
IdS = {}

if not fs.exists("PdsWaid") then
fileCheck = fs.open("PdsWaid", "w")
for i, k in ipairs(Passwords) do
fileCheck.writeLine(Passwords[k])
end
fileCheck.close()
elseif fs.exists("PdsWaid") then
local fileRead = fs.open("PdsWaid", "r")  -- Error here...
local lines1 = fileRead.readLine()
repeat
table.insert(Passwords, lines1)
lines1 = fileRead.readLine()
until lines1 == nil

fileRead.close()
end

if not fs.exists("PdsPaid") then
fileChecks = fs.open("PdsPaid", "w")
for h, g in ipairs(IdS) do
fileChecks.writeLine(IdS[k])
end
fileChecks.close()
else
fileReads = fs.open("PsdsPaid", "r")
lines2 = fileReads.readLine()
repeat
table.insert(IdS, lines2)
lines2 = fileReads.readLine()
until lines2 == nil
fileReads.close()
end



function checkInput()
while running do
event, key = os.pullEvent("key")
if event == "key" then
if key == keys.Q then
running = false
end
end
end
end

function Server()
while runningServer do
id, msg = rednet.receive()
for i, v in ipairs(IdS) do
if id == IdS[v] then
for k, f in ipairs(Passwords) do
if msg == Passwords[f] then
rednet.send(id, "ValidPassword")
end
end
end
end
end
end


function LoadServer()
clear(1,1)
TSP("Loading Password Server v. 2.3")
sleep(1)
TSP("Loaded... Ready to Go...")
clear(1,10)
TSP("Type "Q" to exit")
sleep(2)
for i = 1, 9 do
term.setCursorPos(1, i)
term.clearLine()
end
parallel.waitForAny(Server, checkInput)
end

while runningP do
clear(1,1)
TSP("Welcome.....")
p("-----------------")
sleep(2)
TSP("Loading components...")
p("-----------------")
sleep(3)
TSP("Done!")
sleep(0.5)
clear(1,1)
TSP("Enter Password to boot server...")
sleep(1)
TSW("Password > ")
PassInput = read("#")
if PassInput == "ServerBootAccess_Key" then
clear(1,1)
runningP = false
LoadServer()
else
clear(1,1)
TSP("Wrong password ?")
sleep(4)
os.shutdown()
end
end
os.pullEvent = oldDog
end
Pastebin code [full code]:
Spoilerhttp://pastebin.com/huQQYWLc

Thanks in advance :-P

____________________________

-Mikk809h

ComputerCraft is awesome
remiX #2
Posted 24 November 2012 - 10:45 AM
I think you forgot an 'end' ;)/>/>

function PassServer()
    local oldDog = os.pullEvent
    os.pullEvent = os.pullEventRaw
    local runningP = true
      
    Passwords = {}
    IdS = {}

    if not fs.exists("PdsWaid") then
        fileCheck = fs.open("PdsWaid", "w")
        for i, k in ipairs(Passwords) do
            fileCheck.writeLine(Passwords[k])
        end
        fileCheck.close()
    elseif fs.exists("PdsWaid") then
        local fileRead = fs.open("PdsWaid", "r")  -- Error here...
        local lines1 = fileRead.readLine()
        repeat
            table.insert(Passwords, lines1)
            lines1 = fileRead.readLine()
        until lines1 == nil
        fileRead.close()
    end
end

If that is not the problem add this line after it attempts to open the file:

        if not fileRead then error("Error opening file.") end
To check if it actually opens it.
Goof #3
Posted 25 November 2012 - 12:19 AM
i dont need an end.. PassServer is the whole server progress.. so its better to read… but when i entered the if not fileRead then error("Error opening file.") end after local fileRead = fs.open("PdsWaid", "r") then it does not show op.. but the same error still came up (attempt to call nil)
billysback #4
Posted 25 November 2012 - 03:40 AM
have you accidentally set "fs" to anything at any point?
huettner94 #5
Posted 25 November 2012 - 03:48 AM
fs is a build in API for Filesystem Operations. It's loaded by default
billysback #6
Posted 25 November 2012 - 04:08 AM
I know, but can't it still be overridden, by doing fs = "EXAMPLE", or is it not a global variable?
Doyle3694 #7
Posted 25 November 2012 - 04:17 AM
ofcourse it can be overwritten, thats why you can make your own fs without modifying ROM
billysback #8
Posted 25 November 2012 - 04:19 AM
Then that's my point, has he overridden it by accident during his code?
Doyle3694 #9
Posted 25 November 2012 - 04:58 AM
-snip-
Goof #10
Posted 25 November 2012 - 05:24 AM
But how can i then do it with the IO file systems?
Goof #11
Posted 25 November 2012 - 07:47 AM
Deleted…