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

Why is user nil?

Started by X3ME, 13 March 2016 - 12:13 PM
X3ME #1
Posted 13 March 2016 - 01:13 PM

-- Functions
local function cprint(text)
  local x,y = term.getSize()
  local x2,y2 = term.getCursorPos()
  term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  write(text)
end
local function clear()
  term.clear()
  term.setCursorPos(1,1)
end
local function drawscreen()
  write(user"@Reactor 1> ")
  input = read()
end
local function main()
drawscreen()
if input == "help" then
  write("List of avaiable commands:\n")
  write("  start	  - Start the Reactor ( Requires Elevated Privileges )\n")
  write("  stop	   - Stop the Reactor ( Requires Elevated Privileges )\n")
  write("  state	  - Reload Reactor State \n")
  write("  networkoff - Disconnect the Reactor from the network ( Requires Super-Elevated Privileges )\n")
  write("  reboot	 - Reboot this computer \n")
  write("  version    - Display the version number and copyright information\n")
end
if input == "start" then
end
if input == "stop" then
end
if input == "state" then
end
if input == "networkoff" then
clear()
cprint("Are you SURE you wish to perform this?")
write("\n")
cprint("N/y>")
input2 = read()
if input2 == "y" then
  print("boom")
else
  print("Nothing was done")
  sleep(2)
  clear()
end
end
if input == "reboot" then
write("Goodbye")
os.reboot()
end
if input == "version" then
write("Hiperbolt HiperReactor 0.1\n")
write("\n")
write("Copyright (C) 2016 hiperbolt\n")
write("\n")
end
main()
end
-- init
local tArgs = { ... }
if #tArgs < 1 then
  user = guest
  clear()
  main()
end
local sUser = tArgs[1]
if sUser == "root" then
  clear()
  write("Enter root password:")
  input = read("*")
  if input == "root" then
    clear()
    user = root
    main()
  end
elseif sUser == "heda" then
clear()
  write("Enter heda password:")
  input = read("*")
  if input == "heda" then
    clear()
    user = heda
    main()
  end
end

Why is user nil in main() if its set as gues or root or heda in init?
Lupus590 #2
Posted 13 March 2016 - 02:32 PM
I did a a check on your indenting, it main meant to call itself? A while true loop would be better

Will edit this post as I find more errors.

Edit: I don't see any other errors.
Edited on 13 March 2016 - 01:35 PM
X3ME #3
Posted 13 March 2016 - 02:39 PM
Yeah I will do that, will that fix my problem?
KingofGamesYami #4
Posted 13 March 2016 - 03:12 PM
I'm going to pull a small part of your script out to demonstrate the problem, keep in mind that this problem exists in multiple places.


elseif sUser == "heda" then --#if they entered "heda" (fine so far)
  clear() --#do some stuff
  write("Enter heda password:")
  input = read("*")
  if input == "heda" then --#if the input was "heda" again (also fine)
    clear()
    user = heda --#set user equal to the variable heda, which is not defined anywhere in the script.
                --#in other words, set user equal to nil
    main()
  end
end
Edited on 13 March 2016 - 02:12 PM
X3ME #5
Posted 13 March 2016 - 03:16 PM
I'm going to pull a small part of your script out to demonstrate the problem, keep in mind that this problem exists in multiple places.


elseif sUser == "heda" then --#if they entered "heda" (fine so far)
  clear() --#do some stuff
  write("Enter heda password:")
  input = read("*")
  if input == "heda" then --#if the input was "heda" again (also fine)
	clear()
	user = heda --#set user equal to the variable heda, which is not defined anywhere in the script.
				--#in other words, set user equal to nil
	main()
  end
end

Oh god I'm dumb, I have set the variable to a nil variable, i forgot the ""…
Thanks KingofGamesYami!