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

Compare nil with number error

Started by Creator13, 17 July 2012 - 08:57 AM
Creator13 #1
Posted 17 July 2012 - 10:57 AM
Hi everyone,
My computer gives the error: attempt to compare nil with number (at line 38) in this code (it's a door lock with tries):

function clear()
	term.clear()
	term.setCursorPos(1,1)
end
function tries()
	triesLeft = triesLeft - 1
end
function enterPass()
	write("Password: ")
	password = read("*")
end
function correct()
	print("Password correct!")
	rs.setOutput("back", true)
	sleep(4)
	rs.setOutput("back", false)
end
tiresLeft = 3
os.pullEvent = os.pullEventRaw
term.redirect(peripheral.wrap("right"))
	clear()
	print("Enter password to open")
term.restore()
clear()
print("Enter password to open")
enterPass()
if password == "Mypass" then
	clear()
	correct()
else
	while password ~= "Mypass" and triesLeft >= 0 do
		clear()
		print("Password incorrect, try again")
		print("Tries left: "..triesLeft)
		enterPass()
		tries()
	end
	if triesLeft == 0 then
		print("You have no tries left")
		while true do
			print("")
		end
	elseif password == "Mypass" then
		correct()
	end
end
The prgram starts good, but when it enters the loop at line 38 it gives the error. Also, when you enter the correct password, it works without errors.
I know what the error means, but I have no idea what I'm doing wrong…
Pinkishu #2
Posted 17 July 2012 - 11:25 AM
tiresLeft = 3

you might want to fix that up into triesLeft = 3
Creator13 #3
Posted 17 July 2012 - 11:30 AM
thanks, I really wouldnt have seen that :/. I first didnt even see the difference…