Posted 03 September 2013 - 06:08 AM
I keep getting this error and after some time, can't work out whats causing it, the same code works earlier.
client:301: attempt to index ? (a nil value) Code bellow or http://pastebin.com/XsQe1rci
client:301: attempt to index ? (a nil value) Code bellow or http://pastebin.com/XsQe1rci
os.loadAPI("crypt")
os.loadAPI("gui")
local textStart = gui.textStart
local modemSide = "back"
local diskSide = "right"
local serverId = 1
local withdrawId = 2
local version = "CC-Bank-0.2.0-Compatible"
-- Import GUI functions
local drawHeader, drawButtons, drawButton, drawError, center = gui.drawHeader, gui.drawButtons, gui.drawButton, gui.drawError, gui.center
local waitForMouse, waitForButton, waitForChar, waitForKey, waitForEnter = gui.waitForMouse, gui.waitForButton, gui.waitForChar, gui.waitForKey, gui.waitForEnter
local waitForDisk = gui.waitForDisk
function otherAmount()
local amount = ""
term.clear() drawHeader()
term.setCursorPos(center(textStart, "Amount : MC$ ") + 12, textStart)
return read()
end
function balance(acc, pin)
term.clear() drawHeader() drawButtons()
drawButton(4, "Cancel")
drawButton(5, "MC Dollars")
local mult = nil
while true do
local button = waitForButton()
if button == 4 then return
elseif button == 5 then mult = 1 break
end
end
local senderId, response = nil, nil
rednet.open(modemSide)
rednet.send(serverId, version .. textutils.serialize( { "BALANCE", acc, crypt.hashPassword(pin) } ) )
while true do
senderId, response = rednet.receive(5)
if senderId == serverId then break end
end
rednet.close(modemSide)
term.clear() drawHeader() drawButtons()
drawButton(8, "OK")
if response ~= "-" then
local displayBalance = math.floor(tonumber(response) * mult * 100) / 100
center(textStart, "Balance: " .. displayBalance)
else
drawError("Transaction Error")
end
while true do
local button = waitForButton()
if button == 8 then return end
end
end
function transfer(acc, pin)
term.clear() drawHeader() drawButtons()
drawButton(1, "MC$ 100")
drawButton(2, "MC$ 200")
drawButton(3, "MC$ 500")
drawButton(5, "MC$ 1000")
drawButton(6, "MC$ 2000")
drawButton(7, "MC$ 5000")
drawButton(4, "Cancel")
drawButton(8, "Other Amount")
local amountS = nil
while true do
local button = waitForButton()
if button == 1 then amountS = "100" break
elseif button == 2 then amountS = "200" break
elseif button == 3 then amountS = "500" break
elseif button == 5 then amountS = "1000" break
elseif button == 6 then amountS = "2000" break
elseif button == 7 then amountS = "5000" break
elseif button == 4 then return
elseif button == 8 then amountS = otherAmount() break
end
end
term.clear() drawHeader() drawButtons()
local amount = tonumber(amountS)
if amount ~= nil and (amount % 1) == 0 then
term.clear() drawHeader()
term.setCursorPos(center(textStart, "Amount : MC$ ") + 12, textStart)
term.write(amountS)
term.setCursorPos(center(textStart + 1, "Account : ") + 10, textStart + 1)
local account = read()
drawButtons()
drawButton(4, "Cancel")
drawButton(8, "Confirm")
while true do
local button = waitForButton()
if button == 4 then return
elseif button == 8 then break
end
end
local senderId, response = nil, nil
rednet.open(modemSide)
rednet.send(serverId, version .. textutils.serialize( { "TRANSFER", acc, crypt.hashPassword(pin), account, amountS } ) )
while true do
senderId, response = rednet.receive(5)
if senderId == serverId then break end
end
rednet.close(modemSide)
term.clear() drawHeader() drawButtons()
if response ~= "-" then
center(textStart, "Transfer Successful")
else
drawError("Transaction Error")
end
else
term.clear() drawHeader() drawButtons()
drawError("Invalid Amount")
--[[
while true do
local button = waitForButton()
if button == 8 then return end
end
--]]
end
drawButton(8, "OK")
while true do
local button = waitForButton()
if button == 8 then return end
end
end
function withdraw(acc, pin)
term.clear() drawHeader() drawButtons()
drawButton(1, "MC$ 100")
drawButton(2, "MC$ 200")
drawButton(3, "MC$ 500")
drawButton(5, "MC$ 1000")
drawButton(6, "MC$ 2000")
drawButton(7, "MC$ 5000")
drawButton(4, "Cancel")
drawButton(8, "Other Amount")
local amountS = nil
while true do
local button = waitForButton()
if button == 1 then amountS = "100" break
elseif button == 2 then amountS = "200" break
elseif button == 3 then amountS = "500" break
elseif button == 5 then amountS = "1000" break
elseif button == 6 then amountS = "2000" break
elseif button == 7 then amountS = "5000" break
elseif button == 4 then return
elseif button == 8 then amountS = otherAmount() break
end
end
term.clear() drawHeader() drawButtons()
local amount = tonumber(amountS)
if amount ~= nil and (amount % 100) == 0 then
term.clear() drawHeader()
term.setCursorPos(center(textStart, "Amount : MC$ ") + 12, textStart)
term.write(amountS)
drawButtons()
drawButton(4, "Cancel")
drawButton(8, "Confirm")
while true do
local button = waitForButton()
if button == 4 then return
elseif button == 8 then break
end
end
local senderId, response = nil, nil
rednet.open(modemSide)
rednet.send(serverId, version .. textutils.serialize( { "WITHDRAW", acc, crypt.hashPassword(pin), amountS } ) )
while true do
senderId, response = rednet.receive(5)
if senderId == serverId then break end
end
rednet.close(modemSide)
term.clear() drawHeader()
if response ~= "-" then
center(textStart, "Withdrawing " .. (amount) .. " MC$")
rednet.open(modemSide)
rednet.send(withdrawId, version .. textutils.serialize( { "WITHDRAW", (amount) .. "" } ) )
while true do
senderId, response = rednet.receive(5)
if senderId == withdrawId then break end
end
rednet.close(modemSide)
term.clear() drawHeader()
if response ~= "-" then
center(textStart, "Please take your cash")
sleep(2)
return
else
drawButtons()
drawError("Withdrawal Error")
end
else
drawError("Transaction Error")
end
else
term.clear() drawHeader() drawButtons()
drawError("Invalid Amount")
--[[
while true do
local button = waitForButton()
if button == 8 then return end
end
--]]
end
drawButton(8, "OK")
while true do
local button = waitForButton()
if button == 8 then return end
end
end
function bank(acc, pin)
while true do
term.clear() drawHeader() drawButtons()
drawButton(4, "Return Card")
drawButton(5, "Balance Check")
drawButton(6, "Withdrawal")
drawButton(7, "Deposit")
drawButton(8, "Transfer")
local button = waitForButton()
if button == 4 then return
elseif button == 5 then balance(acc, pin)
elseif button == 6 then withdraw(acc, pin)
--elseif button == 7 then deposit(acc, pin)
elseif button == 8 then transfer(acc, pin)
end
end
end
function begin()
local acc, pin
local versions = "CC-Bank-0.2.0-Compatible"
term.clear() drawHeader()
center(textStart, "Insert keycard or press ENTER")
center(textStart + 1, "to transact without a keycard")
parallel.waitForAny(waitForEnter, waitForDisk)
term.clear() drawHeader()
term.setCursorPos(center(textStart, "Account No : ") + 13, textStart)
if disk.hasData(diskSide) then
acc = disk.getLabel(diskSide)
term.write(acc)
else
acc = read()
end
term.setCursorPos(center(textStart + 1, "PIN : ") + 6, textStart + 1)
pin = read("*")
sleep(1)
local senderIdm, responsem = nil, nil
rednet.open(modemSide)
rednet.send(serverId, versions .. textutils.serialize( {"LOGIN", acc, crypt.hashPassword(pin) } ) )
while true do
senderIdm, responsem = rednet.receive(5)
if senderIdm == serverId then break end
end
rednet.close(modemSide)
term.clear() drawHeader()
if responsem ~= "-" then
bank(acc, pin)
term.clear() drawHeader()
disk.eject(diskSide)
center(textStart, "Thank you for banking with BM")
center(textStart + 1, "Please take your keycard")
sleep(2)
else
drawError("Login Error")
end
while true do
local button = waitForButton()
if button == 8 then return end
end
end
while true do
begin()
end