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

attempt to index ? (a nil value)

Started by K_Schumacher, 13 September 2012 - 01:20 AM
K_Schumacher #1
Posted 13 September 2012 - 03:20 AM
Hey I can not figure this on out. I get a attempt to index ? (a nil value) on line 151. I have been looking at it for 2 days now and don't see a problem. Any help would be great.
Edit: The password-config file has {[1]=1, [2]="admin", [3]="password", [4]=10, [5]=120, [6]="back", [7]=10,} to start with then gets edited by the program below.

local version = 1.86
local pullEvent = os.pullEvent
local file = fs.open("/rom/programs/password-config","r")
local config_table = textutils.unserialize(file.readLine())
file.close()
--
print(config_table)
for i =1,#config_table do
print(config_table[i])
end
print("config was loaded!")
sleep(5)
--
local main = true
local programed = config_table[1]
local adminpassword = config_table[2]
local userpassword = config_table[3]
local default_attempts = config_table[4]
local timeout = config_table[5]
local rsside = config_table[6]
local waittime = config_table[7]
function UpdateUI(status)
term.clear()
term.setCursorPos(1, 1)
print("Password-Advanced v",tostring(version))
  if status < 6 then
   print("Ctrl-T is enabled when in admin setings.")
  end
  if status == 0 then
  
   print("Admin password is used to access setting.")
   term.write("Enter Admin Password:")
  end
  if status == 1 then
   print("User password is used to activate the redstone signal.")
   term.write("Enter User Password:")
  end
  if status == 2 then
   print("Limit of times you can attempted to loging in.")
   term.write("Enter Number of Times:")
  end
  if status == 3 then
   print("Time that users will be locked out after.")
   term.write("Enter Seconds:")
  end
  if status == 4 then
   print("Side that the redstone signal will come out.")
   term.write("Enter Side:")
  end
  if status == 5 then
   print("Time that redstone signal will stay high.")
   term.write("Enter Seconds:")
  end
  if status == 6 then
   term.write("Enter Password:")
  end
  if status == 7 then
   term.write("Setting Reseting!...")
  end
  if status == 8 then
   term.write("Access Granted!...")
  end
  if status == 9 then
term.write("Access Denied!...")
  end
  if status == 10 then
term.write("Exceeded Number of attempts!... (locked out)")
  end
end
function RsSignal(rs_side,wait_time)
rs.setOutput(rs_side,true)
sleep(wait_time)
rs.setOutput(rs_side,false)
end
UpdateUI(6)
while main do
if programed == 0 then -- admin settings
  os.pullEvent = pullEvent --Ctrl-t enabled
  while true do
   UpdateUI(0)
   local input = read()
	if input ~= nil then
	 adminpassword = tostring(input)
	 config_table[2] = adminpassword
	 break  
end
  end
  while true do
   UpdateUI(1)
   local input = read()
   if input ~= nil then
	userpassword = tostring(input)
config_table[3] = userpassword
	break  
   end
  end
  while true do
   UpdateUI(2)
   local input = read()
   input = tonumber(input)
	if input ~= nil then
	 default_attempts = input
  config_table[4] = default_attempts
  local current_attempts = input
  break
end  
  end
  while true do
   UpdateUI(3)
   local input = read()
   input = tonumber(input)
	if input ~= nil then
	 timeout = input
  config_table[5] = timeout
  break
end
  end
  while true do
   UpdateUI(4)
   local input = read()
   if input ~= nil then
	rsside = tostring(input)
	config_table[6] = rsside
	break  
   end
  end
  while true do
   UpdateUI(5)
   local input = read()
   input = tonumber(input)
	if input ~= nil then
	  waittime = input
   config_table[7] = waittime  
   break
end	  
  end
programed = 1
config_table[1] = 1  
os.pullEvent = os.pullEventRaw -- Ctrl-T disabled
local file = fs.open("/rom/programs/password-config","w")
file.writeLine(textutils.serialize(config_table)) --attempt to index ? (a nil value)
  --
print(config_table)
for i = 1,#config_table do
  print(config_table[i])
end
print("config was saved!")
sleep(5)
UpdateUI(6)
--
end
local input = read("*")
if current_attempts ~= 0 then
  if input ~= adminpassword then
   if input ~= userpassword then
	UpdateUI(9) --failed
	sleep(2)
	current_attempts = current_attempts - 1
	UpdateUI(6) --password
   else
	UpdateUI(8) -- urser login
	RsSignal(rsside,waittime)
	current_attempts = default_attempts
	UpdateUI(6) -- password
   end
  else
   UpdateUI(7) -- admin setings
   sleep(2)
   programed = 0
  end
else
  UpdateUI(10) -- locked out
  sleep(timeout)
  current_attempts = default_attempts
  UpdateUI(6) -- password
end

end
Kadecamz #2
Posted 13 September 2012 - 04:08 AM
Please tell where line 151 is, as this lua showing does not list lines. :)/>/>

EDIT: Just try removing random characters, sometimes that will work.
K_Schumacher #3
Posted 13 September 2012 - 04:14 AM
file.writeLine(textutils.serialize(config_table)) attempt to index ? (a nil value)
it is on the last screen
Kilobyte #4
Posted 13 September 2012 - 07:57 AM
You can't write to the rom folder so file.open will return nil.
To solve this change the config file path to something else (like just "password-config")

greets Stiepen/Kilobyte

EDIT: yay 20th post (i know thats nearly nothing but still i'm happy now :)/>/>)
K_Schumacher #5
Posted 13 September 2012 - 09:15 PM
Thanks that was the main problem it is now writing nothing to the file but that i can figure out.

Thanks Again