Jump to content




Giving error when calling function


  • You cannot reply to this topic
3 replies to this topic

#1 okok99haha

  • Members
  • 24 posts

Posted 02 July 2014 - 03:15 PM

Hello,
Continuing the same problem from the last post, it gives me error when I am calling the function newID.
I am not sure what change I made after last time running the function, and for me it does not seem wrong.
the error is attempt to call number on line 96, which is in the function of mainMenu, newID().
http://pastebin.com/buswPGHh, if you think it is easier to see it on the pastebin.
Thanks in advance,

local diskDriveSide = "left"

function RandomString()
  length = 16
  if length < 1 then return nil end
  local array = {}
  for i=1, length do
	array[i] = string.char(math.random(32,126))
end
  return table.concat(array)
end

function compare()
  local disk_f = io.open("disk/pw.txt", "r")
  if disk_f == nil then
	term.setCursorPos(1,4)
	print("File or code does not exist.")
	sleep(3)
   else
  local disk_pw = disk_f:read("*l")
  disk_f:close()
  local lastID_f = io.open("lastid.txt", "r")
  local lastID = lastID_f:read("*l")
  lastID_f:close()
  while true do
  term.setCursorPos(1,4)
  print("Checking..")
  sleep(0.3)
	for fileNumber = 1, lastID do
	  print("Checking : "..fileNumber..".txt")
	  local check_f = io.open(fileNumber..".txt","r")
	  local line = check_f:read("*l")
	  sleep(0.1)
	if line == disk_pw then
	  local nick = check_f:read("*l")
	  print("Username: " ..nick)
	  print("Press any key to exit")
	  os.pullEvent()
	  return
	end
	end
	print("Invalid, press any key to exit.")
	os.pullEvent()
	return
end
end

function newID()
  local genCode = RandomString()
  if disk.isPresent(diskDriveSide) == false then
	term.setCursorPos(1,4)
	print("Insert Disk!")
	sleep(3)
   else
	term.setCursorPos(1,4)
	term.write("Please type in the player name : ")
	local playerName = read()
	disk.setLabel(diskDriveSide, "SPCard - " ..playerName)
	local disk_pw = io.open("disk/pw.txt", "w")
	disk_pw:write(genCode)
	disk_pw:close()
	local root_lastID = io.open("lastid.txt","r")
	local lastID = root_lastID:read("*l")
	root_lastID:close()
	root_lastID = io.open("lastid.txt","w")
	newID = lastID+1
	root_lastID:write(newID)
	root_lastID:close()
	local root_newID = io.open(newID..".txt","w")
	root_newID:write(genCode)
	root_newID:write("\n")
	root_newID:write(playerName)
	root_newID:close()
	term.setCursorPos(1,6)
	print("Generated!")
	sleep(2)
  end
  end
end

function mainMenu()
  term.clear()
  term.setCursorPos(1,1)
  print("1. Verify")
  print("2. Make New ID Card")
  local event, key = os.pullEvent("char")
  if key == "1" then
	 if disk.isPresent(diskDriveSide) == false then
		term.setCursorPos(1,4)
		print("Insert Disk!")
		sleep(3)
	  else
	   compare()
	 end
   elseif key == "2" then
	 newID()
   else
	 sleep(0.1)
end
end

while true do
  mainMenu()
end

Edited by okok99haha, 02 July 2014 - 03:35 PM.


#2 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 02 July 2014 - 08:28 PM

It seems you're overriding the function newID so that it becomes a number
function newID()
	...
	newID = lastID + 1
	...
end
so you can fix this error by giving the variable in your function another name
function newID()
	...
	new_id = lastID + 1
	...
end
You should look into using locals aswell since it's bad to have globals unless you plan on accessing these variables/functions from other files
http://www.lua.org/pil/4.2.html

Edited by TheOddByte, 02 July 2014 - 08:28 PM.


#3 okok99haha

  • Members
  • 24 posts

Posted 03 July 2014 - 08:24 AM

View PostTheOddByte, on 02 July 2014 - 08:28 PM, said:

It seems you're overriding the function newID so that it becomes a number
function newID()
	...
	newID = lastID + 1
	...
end
so you can fix this error by giving the variable in your function another name
function newID()
	...
	new_id = lastID + 1
	...
end
You should look into using locals aswell since it's bad to have globals unless you plan on accessing these variables/functions from other files
http://www.lua.org/pil/4.2.html

Changed function name to new_ID but does not work.
attept to call nil. Any solution?

#4 okok99haha

  • Members
  • 24 posts

Posted 03 July 2014 - 08:52 AM

View PostTheOddByte, on 02 July 2014 - 08:28 PM, said:

It seems you're overriding the function newID so that it becomes a number
function newID()
	...
	newID = lastID + 1
	...
end
so you can fix this error by giving the variable in your function another name
function newID()
	...
	new_id = lastID + 1
	...
end
You should look into using locals aswell since it's bad to have globals unless you plan on accessing these variables/functions from other files
http://www.lua.org/pil/4.2.html

Not sure but did some changes and works now
Thanks! :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users