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

Attempt to index ? (a nil value)

Started by Zyfox, 19 December 2017 - 10:43 PM
Zyfox #1
Posted 19 December 2017 - 11:43 PM
Hello CC
im getting this error "Attempt to index ? a nil value"

this is the code :

Spoiler
os.loadAPI("sha")

local passPath = "passwords" --Change this if you want a different password file

if not fs.exists(passPath) then --Create a password file
	local f = fs.open(passPath, "w")
	f.write(textutils.serialize({}))
	f.close()
end

local function secureInput( mask, prestring )
	local l = true
	term.setCursorBlink(true)
	local prestring = prestring or ""
	local str = ""
	local sx, sy = term.getCursorPos()
	while l do
		local e, a, b, c, d = os.pullEventRaw()
		if e == "char" then
			str = str .. a
		elseif e == "key" then
			if a == 14 then
				str = str:sub(1, -2)
			elseif a == 28 then
				l = false
				term.setBackgroundColour( colours.black )
				term.setTextColour( colours.white )
				print()
				return str
			end
		end
		term.setCursorPos(sx, sy)
		term.setBackgroundColour( colours.white )
		term.setTextColour( colours.black )
		term.clearLine()
		if mask then
			write( prestring .. " > " .. string.rep(mask, #str) )
		else
			write( prestring .. " > " .. str )
		end
	end
end

local function getPass( usr )
	local f = fs.open(passPath,"r")
	local pwds = textutils.unserialize( f.readAll() )
	f.close()
	if pwds[ usr ] then
		return pwds[ usr ]
	else
		return false
	end
end

local function login( usr, pwd )
	local info = getPass( usr )
	if info then
		if sha.sha256( pwd .. info.salt ) == info.pwd then
			return true
		else
			return false
		end
	else
		return false
	end
end

--Login script--
local loop = true
while loop do
	local username = secureInput( nil, "username" )
	local password = secureInput( "x", "password" )
	if login( username, password ) then
		print("Login successfull!")
		loop = false
		break
		--DO SOMETHING
	else
		print("Login failed.")
		--REMOVE these 2 lines below so that the loop continues after incorrect login, once proven program works.
		loop = false
		break
	end
end

this code isnt mine!!!
it belongs to CompuTech
but i am getting this error and it points on this line :
write( prestring .. " > " .. string.rep(mask, #str) )
else
write( prestring .. " > " .. str )

i am kind of new to CC and to lua , if someone can help me to figure it out thanks <3 and srry for bothering
Edited by
Lupus590 #2
Posted 20 December 2017 - 12:09 PM
http://www.computercraft.info/forums2/index.php?/topic/28833-writing-a-good-ask-a-pro-post/

"Attempt to index ? a nil value" means that you are trying to use something as a table which has not been initualised. It's posible that you have made a spelling mistake or have don't have the 'program flow' correct.