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

[Checking Data From Tables] Ask A Pro [Season nil]

Started by JustIan12, 04 September 2017 - 10:27 AM
JustIan12 #1
Posted 04 September 2017 - 12:27 PM
Tables, seem to be a recurring topic in my posts but I am almost done with my code I just need to finish it but I do not have a clue on how I can do this.

I have created a main loop (At the bottom of the code) which should get data from a table then compare it to the whitelist table then if the player detected from the sensor is equal to the whitelist then it will do nothing and continue the loop however if the player is not on the whitelist something else will hapen My main issue is that I do not know how to do this and if I need to have both tables unserialized or serialised to compare the data. I don't even know how to do this. or where to begin.


Spoiler
-- Code Written by Ian
-- Before using you need to manually add and assign a table to the WL file by removing the commented line then re adding it.
-- Bootstrapper
term.clear()
term.setTextColor(colors.cyan)
term.setCursorPos(12,4)
print("M.E Defence System")

term.setTextColor(colors.blue)
term.setCursorPos(16 ,7)
print("[Continue]")

os.pullEvent("mouse_click")

term.clear()
term.setCursorPos(15,5)
textutils.slowPrint("Activating")
sleep(1)

local x = 14
while x < 25 do
	paintutils.drawPixel(x,7, colors.green)
	sleep(0.2)
	x = x + 1
end

-- breakInterface Function
function breakInterface()

term.clear()
term.setCursorPos(12,5)
term.setTextColor(colors.white)
  write("Defence Operational")
term.setCursorPos(8,7)
  write("Code > ")

local code = read()

if code == "Safe" then
  term.clear()
  term.setCursorPos(10,7)
   print("Protection Deactivated")
  sleep(1)
  os.shutdown()

elseif code == "Vent" then
  venting()

elseif code == "List" then
  list()

else
  term.clear()
  term.setCursorPos(10,7)
  printError("Command Not Recognised")
  sleep(1)
  breakInterface()
end
end
-- venting Function
function venting()
term.clear()
  term.setCursorPos(12,5)
  term.setTextColor(colors.white)
  write("Are you sure?")
  term.setCursorPos(8,7)
  write("[Y/N] > ")

  local code = read()

  if code == "Y" then
   term.clear()
   term.setCursorPos(17,5)
   textutils.slowPrint("Venting")
	for i = 0,4 do
	 rs.setOutput("back", true)
	 sleep(0.1)
	 rs.setOutput("back", false)
	 sleep(6)
	end
   term.clear()
   term.setCursorPos(1,1)
   shell.run("delete ME")
   os.shutdown()

  elseif code == "N" then
   term.clear()
   term.setCursorPos(12,5)
   textutils.slowPrint("Venting Stopped")
   sleep(1)
   breakInterface()

  else
   term.clear()
   term.setCursorPos(10,7)
   printError("Command Not Recognised")
   sleep(1)
   venting()

  end
end
-- whitelist function
function list()
-- Save

local function save(whitelist,WL)
  local file = fs.open("WL","w")
  file.write(textutils.serialize(whitelist))
  file.close()
end

-- Load

local function pull(WL)
  local file = fs.open("WL","r")
  local data = file.readAll()
  file.close()
  return textutils.unserialize(data)
end


-- Variables

whitelist = {}
whitelist = pull(WL) -- A table needs to exist before using this
sensor = peripheral.wrap("left")
term.clear()
term.setCursorPos(2,2)
  write("Whitelisted Players [Max 3]")
sleep(1)

for i = 1,3 do
  term.setCursorPos(2,i+3)
  textutils.slowPrint(whitelist[i])
  sleep(0.2)
end

term.setCursorPos(2,8)
  write("[Add/Remove] > ")

  local code = read()

if code == "Add" then
  term.clear()
  term.setCursorPos(2,2)
   write("Insert Player IGN > ")

  local IGN = read()

  table.insert(whitelist,IGN)

  save(whitelist,WL)

  term.clear()
  term.setCursorPos(2,2)
  write(IGN.." has been whitelisted.")
  sleep(2)
  breakInterface()

elseif code == "Remove" then
  term.clear()
  term.setCursorPos(2,2)
   write("Whitelisted Players [Max 3]")
  sleep(1)

  for i = 1,3 do
   term.setCursorPos(2,i+3)
   textutils.slowPrint(whitelist[i].."	"..i)
   sleep(0.2)
  end

  term.setCursorPos(2,8)
   write("Insert Player Number > ")

  local IGN = read()
  table.remove(whitelist,IGN)

  save(whitelist,WL)

  term.clear()
  term.setCursorPos(2,2)
   write("Whitelisted Players [Max 3]")
  sleep(1)
  for i = 1,3 do
   term.setCursorPos(2,i+3)
   textutils.slowPrint(whitelist[i])
   sleep(0.2)
  end
  sleep(2)
  breakInterface()

  else
   term.clear()
   term.setCursorPos(10,7)
   printError("Command Not Recognised")
   sleep(1)
   breakInterface()
  end
end

-- mainDefence Function
function mainDefence()
while true do
  local detect = textutils.serialize(sensor.getPlayers())



end
end


breakInterface()

Any help will be greatly appreciated.

Thanks

- Ian
Edited on 04 September 2017 - 08:23 PM
KingofGamesYami #2
Posted 04 September 2017 - 01:34 PM
Right, so you need to have unserialized tables (ei they are tables, not strings!) in order to compare them properly.

I highly recommend having a whitelist in the format

t[allowedPlayerName] = true

This much simplifies the code for comparing things to the whitelist.

Next, you have a table with names in it, and you want to check if each one is in the whitelist. That's rather easy to do:

for k, v in pairs( tNames ) do --# iterate through the "tNames" table
  if( t[ v ] ) then --# this is where the structure I showed before is handy; I can simply index the 't' table with a name to tell if it's whitelisted
    --# it's whitelisted
  else
    --# it's not whitelisted
  end
end

PS: I did not use variables from your script, or look at the structure of your script much. It's up to you to implement these ideas into your code.
JustIan12 #3
Posted 04 September 2017 - 03:38 PM
Could you explain a bit more the detail the white list format you suggested I tried using this standalone by itself without using my codes variables to try and understand it more but I have never seen before the format you suggested. it returned the error I don't know what an index is or in otherwords what is the diffrence between using whitelist = {} and t[] if there is any at all

loop:5: index expected, got nil

-- Test Defence Loop
whitelist = {"TestPlayer"}
t["TestPlayer"] = true
for k, v in pairs(whitelist) do

if(t[v]) then
  print("Welcome")
  sleep(1)

else
  for i = 1,10 do
   turtle.suckUp()
   turtle.dropDown()
  end

  for i = 0,4 do
   rs.setOutput("back", true)
   sleep(0.1)
   rs.setOutput("back", false)
   sleep(6)
  end
  print("Vented")
end
end

Thanks

Ian
Edited on 04 September 2017 - 01:38 PM
KingofGamesYami #4
Posted 04 September 2017 - 03:59 PM
That's not how you would declare a table, to do that it'd look like this:

t = {["myusername"]=true}

Apologies for short post, am posting from my phone
JustIan12 #5
Posted 04 September 2017 - 04:56 PM
Oh thats fine XD
JustIan12 #6
Posted 04 September 2017 - 06:45 PM
I am one error from completion and your system seems to fail me.

Spoiler
-- Code Written by Ian
-- Before using you need to manually add and assign a table to the WL file by removing the commented line then re adding it.
-- Bootstrapper
term.clear()
term.setTextColor(colors.cyan)
term.setCursorPos(12,4)
print("M.E Defence System")

term.setTextColor(colors.blue)
term.setCursorPos(16 ,7)
print("[Continue]")

os.pullEvent("mouse_click")

term.clear()
term.setCursorPos(15,5)
textutils.slowPrint("Activating")
sleep(1)

local x = 14
while x < 25 do
    paintutils.drawPixel(x,7, colors.green)
    sleep(0.2)
    x = x + 1
end
-- Variables
sensor = peripheral.wrap("left")
-- breakInterface Function
function breakInterface()

term.clear()
term.setCursorPos(12,5)
term.setTextColor(colors.white)
  write("Defence Operational")
term.setCursorPos(8,7)
  write("Code > ")

local code = read()

if code == "Safe" then
  term.clear()
  term.setCursorPos(10,7)
   print("Protection Deactivated")
  sleep(1)
  os.shutdown()

elseif code == "Vent" then
  venting()

elseif code == "List" then
  list()

else
  term.clear()
  term.setCursorPos(10,7)
  printError("Command Not Recognised")
  sleep(1)
  breakInterface()
end
end
-- venting Function
function venting()
term.clear()
  term.setCursorPos(12,5)
  term.setTextColor(colors.white)
  write("Are you sure?")
  term.setCursorPos(8,7)
  write("[Y/N] > ")

  local code = read()

  if code == "Y" then
   term.clear()
   term.setCursorPos(17,5)
   textutils.slowPrint("Venting")
    for i = 0,4 do
	 rs.setOutput("back", true)
	 sleep(0.1)
	 rs.setOutput("back", false)
	 sleep(6)
    end
   term.clear()
   term.setCursorPos(1,1)
   shell.run("delete ME")
   os.shutdown()

  elseif code == "N" then
   term.clear()
   term.setCursorPos(12,5)
   textutils.slowPrint("Venting Stopped")
   sleep(1)
   breakInterface()

  else
   term.clear()
   term.setCursorPos(10,7)
   printError("Command Not Recognised")
   sleep(1)
   venting()

  end
end
-- whitelist function
function list()
-- Save

local function save(whitelist,WL)
  local file = fs.open("WL","w")
  file.write(textutils.serialize(whitelist))
  file.close()
end

-- Load

local function pull(WL)
  local file = fs.open("WL","r")
  local data = file.readAll()
  file.close()
  return textutils.unserialize(data)
end

whitelist = {}
whitelist = pull(WL) -- A table needs to exist before using this
term.clear()
term.setCursorPos(2,2)
  write("Whitelisted Players [Max 3]")
sleep(1)

for i = 1,3 do
  term.setCursorPos(2,i+3)
  textutils.slowPrint(whitelist[i])
  sleep(0.2)
end

term.setCursorPos(2,8)
  write("[Add/Remove] > ")

  local code = read()

if code == "Add" then
  term.clear()
  term.setCursorPos(2,2)
   write("Insert Player IGN > ")

  local IGN = read()

  table.insert(whitelist,IGN)

  save(whitelist,WL)

  term.clear()
  term.setCursorPos(2,2)
  write(IGN.." has been whitelisted.")
  sleep(2)
  breakInterface()

elseif code == "Remove" then
  term.clear()
  term.setCursorPos(2,2)
   write("Whitelisted Players [Max 3]")
  sleep(1)

  for i = 1,3 do
   term.setCursorPos(2,i+3)
   textutils.slowPrint(whitelist[i].."    "..i)
   sleep(0.2)
  end

  term.setCursorPos(2,8)
   write("Insert Player Number > ")

  local IGN = read()
  table.remove(whitelist,IGN)

  save(whitelist,WL)

  term.clear()
  term.setCursorPos(2,2)
   write("Whitelisted Players [Max 3]")
  sleep(1)
  for i = 1,3 do
   term.setCursorPos(2,i+3)
   textutils.slowPrint(whitelist[i])
   sleep(0.2)
  end
  sleep(2)
  breakInterface()

  else
   term.clear()
   term.setCursorPos(10,7)
   printError("Command Not Recognised")
   sleep(1)
   breakInterface()
  end
end
-- mainDefenceLoop Function
function mainDefenceLoop()
for k, v in pairs(sensor.getPlayers()) do

  if(whitelist[v]) then
   parallel.waitForAny(breakInterface,mainDefenceLoop)

  else
   for i = 1,10 do
    turtle.suckUp()
    turtle.dropDown()
   end

   for i = 0,4 do
    rs.setOutput("back", true)
    sleep(0.1)
    rs.setOutput("back", false)
    sleep(6)

   end
   term.clear()
    term.setCursorPos(1,1)
    shell.run("delete ME")
    os.shutdown()
  end
end
end
parallel.waitForAny(breakInterface,mainDefenceLoop)
--breakInterface()

Line 134 and 218 are conflicting when I remove the whole

whitelist = {[] = true }


and put

whitelist = {}


I get the error

ME:218: attempt to index ? (a nil value)


but when I put

whitelist = {[] = true }


I get this error message

bios:14: [string "ME"]:134: unexpected symbol

I am like lowkeydead

Please Halp

- Ian
Edited on 04 September 2017 - 08:22 PM
KingofGamesYami #7
Posted 04 September 2017 - 11:35 PM
You really aren't getting how this works, are you? Alright, here's a working example:


local whitelist = {}

local function addPlayerToWhitelist( playerName )
  whitelist[ playerName ] = true
end

local function isPlayerInWhitelist( playerName )
  return whitelist[ playerName ]
end
JustIan12 #8
Posted 05 September 2017 - 11:32 AM
No, I am not XD I have tried to apply your working example I still do not know how to apply it I know you are probably handing this to me on a silver platter but my brain does not see what this is doing. I have done more research in the effort of not asking again for you to explain something obvious. But here I am.

local whitelist = {}

local function addPlayerToWhitelist( playerName )
  whitelist[ playerName ] = true
end
local function isPlayerInWhitelist( playerName )
  return whitelist[ playerName ]
end
term.clear()
write("IGN: ")
playerName = read()
addPlayerToWhitelist(playerName)
for i = 1,3 do
term.setCursorPos(2,i+3)
textutils.slowPrint(whitelist[i])
sleep(0.2)
end

the function that I called just returns nil on the slowprint so nothing was added to the whitelist. I am still baffled with playerName even though it is the playername and where to actually call these functions.
KingofGamesYami #9
Posted 05 September 2017 - 12:45 PM
Whitelist does not contain any numerical indexes. It only has string indexes, which you can iterate through using:


for k, v in pairs( whitelist ) do
  print( k )
end
JustIan12 #10
Posted 05 September 2017 - 01:57 PM
I still don't know how to use these functions to compare the table received from the sensor with that loaded into the variable whitelist.

-- Whitelist
local whitelist = {}
   whitelist = pull(WL) -- A table needs to exist before using this
-- Add Player
local function addPlayerToWhitelist( playerName )
  whitelist[ playerName ] = true
end
-- Remove Player
local function isPlayerInWhitelist( playerName )
  return whitelist[ playerName ]
end
-- mainDefenceLoop Function
function mainDefenceLoop()
for k, v in pairs(sensor.getPlayers()) do

  if(whitelist[v]) then
   parallel.waitForAny(breakInterface,mainDefenceLoop)

  else
   for i = 1,10 do
    turtle.suckUp()
    turtle.dropDown()
   end
 
   for i = 0,4 do
    rs.setOutput("back", true)
    sleep(0.1)
    rs.setOutput("back", false)
    sleep(6)
   
   end
   term.clear()
    term.setCursorPos(1,1)
    shell.run("delete ME")
    print("ItDied")
    os.shutdown()
  end
end
end
parallel.waitForAny(breakInterface,mainDefenceLoop)

Ian
KingofGamesYami #11
Posted 05 September 2017 - 05:25 PM
In a previous post, you had shown the table format to be:


So it's a couple of nested tables.


for k, v in pairs( sensor.getPlayers() ) do --# for each nearby player ...
  --# k will be a number (1-n)
  --# v will be a table containg two keys
  --# v.uuid is the UUID assigned to a player*
  --# v.name is the name of the player
  if isPlayerInWhitelist( v.name ) then
    --# do things here
  else
    --# do things here
  end
end

*This could also be indexed by v["uuid"], just like any table.
Edited on 05 September 2017 - 03:26 PM
JustIan12 #12
Posted 05 September 2017 - 07:09 PM
The code returns no errors except when I enable line 115 there is a table but it says that pull(WL) is a nill value and does not exist. (Attempt to call nil) which it does on line 133. Even with my name added to the table manually the code automatically in the if statement on line 233 skips to the else. and shuts off. even though I am whitelisted.

-- Code Written by Ian
-- Before using you need to manually add and assign a table to the WL file by removing the commented line then re adding it.

-- Bootstrapper
term.clear()
term.setTextColor(colors.cyan)
term.setCursorPos(12,4)
print("M.E Defence System")

term.setTextColor(colors.blue)
term.setCursorPos(16 ,7)
print("[Continue]")

os.pullEvent("mouse_click")

term.clear()
term.setCursorPos(15,5)
textutils.slowPrint("Activating")
sleep(1)

local x = 14
while x < 25 do
	paintutils.drawPixel(x,7, colors.green)
	sleep(0.2)
	x = x + 1
end
-- Variables
sensor = peripheral.wrap("left")
-- breakInterface Function
function breakInterface()

term.clear()
term.setCursorPos(12,5)
term.setTextColor(colors.white)
  write("Defence Operational")
term.setCursorPos(8,7)
  write("Code > ")

local code = read()

if code == "Safe" then
  term.clear()
  term.setCursorPos(10,7)
   print("Protection Deactivated")
  sleep(1)
  os.shutdown()

elseif code == "Vent" then
  venting()

elseif code == "List" then
  list()

else
  term.clear()
  term.setCursorPos(10,7)
  printError("Command Not Recognised")
  sleep(1)
  breakInterface()
end
end
-- venting Function
function venting()
term.clear()
  term.setCursorPos(12,5)
  term.setTextColor(colors.white)
  write("Are you sure?")
  term.setCursorPos(8,7)
  write("[Y/N] > ")

  local code = read()

  if code == "Y" then
   term.clear()
   term.setCursorPos(17,5)
   textutils.slowPrint("Venting")
	for i = 0,4 do
	 rs.setOutput("back", true)
	 sleep(0.1)
	 rs.setOutput("back", false)
	 sleep(6)
	end
   term.clear()
   term.setCursorPos(1,1)
   shell.run("delete ME")
   os.shutdown()

  elseif code == "N" then
   term.clear()
   term.setCursorPos(12,5)
   textutils.slowPrint("Venting Stopped")
   sleep(1)
   breakInterface()

  else
   term.clear()
   term.setCursorPos(10,7)
   printError("Command Not Recognised")
   sleep(1)
   venting()
  
  end
end

-- whitelist = pull(WL) -- A table needs to exist before using this
whitelist = {}
-- whitelist function
function list()
-- Save

local function save(whitelist,WL)
  local file = fs.open("WL","w")
  file.write(textutils.serialize(whitelist))
  file.close()
end

-- Load

local function pull(WL)
  local file = fs.open("WL","r")
  local data = file.readAll()
  file.close()
  return textutils.unserialize(data)
end

term.clear()
term.setCursorPos(2,2)
  write("Whitelisted Players [Max 3]")
sleep(1)

for i = 1,3 do
  term.setCursorPos(2,i+3)
  textutils.slowPrint(whitelist[i])
  sleep(0.2)
end

term.setCursorPos(2,8)
  write("[Add/Remove] > ")

  local code = read()

if code == "Add" then
  term.clear()
  term.setCursorPos(2,2)
   write("Insert Player IGN > ")

  local IGN = read()

  table.insert(whitelist,IGN)

  save(whitelist,WL)

  term.clear()
  term.setCursorPos(2,2)
  write(IGN.." has been whitelisted.")
  sleep(2)
  breakInterface()

elseif code == "Remove" then
  term.clear()
  term.setCursorPos(2,2)
   write("Whitelisted Players [Max 3]")
  sleep(1)

  for i = 1,3 do
   term.setCursorPos(2,i+3)
   textutils.slowPrint(whitelist[i].."	"..i)
   sleep(0.2)
  end

  term.setCursorPos(2,8)
   write("Insert Player Number > ")

  local IGN = read()
  table.remove(whitelist,IGN)

  save(whitelist,WL)

  term.clear()
  term.setCursorPos(2,2)
   write("Whitelisted Players [Max 3]")
  sleep(1)
  for i = 1,3 do
   term.setCursorPos(2,i+3)
   textutils.slowPrint(whitelist[i])
   sleep(0.2)
  end
  sleep(2)
  breakInterface()

  else
   term.clear()
   term.setCursorPos(10,7)
   printError("Command Not Recognised")
   sleep(1)
   breakInterface()
  end
end
-- Add Player
local function addPlayerToWhitelist(playerName)
  whitelist[playerName] = true
end
-- Check Player
local function isPlayerInWhitelist(playerName)
  return whitelist[playerName]
end
-- mainDefenceLoop Function
function mainDefenceLoop()
for k, v in pairs(sensor.getPlayers()) do

  if isPlayerInWhitelist(v.name) then
   parallel.waitForAny(breakInterface,mainDefenceLoop)

  else
   for i = 1,10 do
	turtle.suckUp()
	turtle.dropDown()
   end

   for i = 0,4 do
	rs.setOutput("front", true)
	sleep(0.3)
	rs.setOutput("front", false)
	sleep(6)
  
   end
   term.clear()
	term.setCursorPos(1,1)
	shell.run("delete ME")
	print("ItDied")
	sleep(3)
	os.shutdown()
  end
end
end

parallel.waitForAny(breakInterface,mainDefenceLoop)

- Ian
Edited on 05 September 2017 - 05:10 PM
KingofGamesYami #13
Posted 05 September 2017 - 09:06 PM
You can't call a function before you define it. Move the function declaration for pull above the first line you attempt to use it.
Also, your project structure is really screwed up. Why do you have parallel.waitForAny scattered everywhere, and why do you keep calling mainDefenceLoop and breakInterface? Especially from within mainDefenceLoop and breakInterface. That is called recursion, and Lua will start throwing Java Exceptions at you if you keep that up.

Edit: I've gone through and restructured your code without making any functionality changes, such that it does not call parallel.waitForAny more than once, and doesn't use recursion… this doesn't implement the function moving, as I will leave that to you to figure out.

Spoiler
-- Code Written by Ian
-- Before using you need to manually add and assign a table to the WL file by removing the commented line then re adding it.

-- Bootstrapper
term.clear()
term.setTextColor(colors.cyan)
term.setCursorPos(12,4)
print("M.E Defence System")

term.setTextColor(colors.blue)
term.setCursorPos(16 ,7)
print("[Continue]")

os.pullEvent("mouse_click")

term.clear()
term.setCursorPos(15,5)
textutils.slowPrint("Activating")
sleep(1)

local x = 14
while x < 25 do
  paintutils.drawPixel(x,7, colors.green)
  sleep(0.2)
  x = x + 1
end
-- Variables
sensor = peripheral.wrap("left")
-- breakInterface Function
function breakInterface()
  while true do
    term.clear()
    term.setCursorPos(12,5)
    term.setTextColor(colors.white)
    write("Defence Operational")
    term.setCursorPos(8,7)
    write("Code > ")
    local code = read()
    if code == "Safe" then
      term.clear()
      term.setCursorPos(10,7)
      print("Protection Deactivated")
      sleep(1)
      os.shutdown()
    elseif code == "Vent" then
      venting()
    elseif code == "List" then
      list()
      return
    else
      term.clear()
      term.setCursorPos(10,7)
      printError("Command Not Recognised")
      sleep(1)
      --breakInterface()
    end
end
-- venting Function
function venting()
  while true do
    term.clear()
    term.setCursorPos(12,5)
    term.setTextColor(colors.white)
    write("Are you sure?")
    term.setCursorPos(8,7)
    write("[Y/N] > ")
    local code = read()
    if code == "Y" then
      term.clear()
      term.setCursorPos(17,5)
      textutils.slowPrint("Venting")
      for i = 0,4 do
        rs.setOutput("back", true)
        sleep(0.1)
        rs.setOutput("back", false)
        sleep(6)
      end
      term.clear()
      term.setCursorPos(1,1)
      shell.run("delete ME")
      os.shutdown()
    elseif code == "N" then
      term.clear()
      term.setCursorPos(12,5)
      textutils.slowPrint("Venting Stopped")
      sleep(1)
      return
    else
      term.clear()
      term.setCursorPos(10,7)
      printError("Command Not Recognised")
      sleep(1)
    end
  end
end

-- whitelist = pull(WL) -- A table needs to exist before using this
whitelist = {}
-- whitelist function
function list()
-- Save

local function save(whitelist,WL)
  local file = fs.open("WL","w")
  file.write(textutils.serialize(whitelist))
  file.close()
end

-- Load

local function pull(WL)
  local file = fs.open("WL","r")
  local data = file.readAll()
  file.close()
  return textutils.unserialize(data)
end

term.clear()
term.setCursorPos(2,2)
write("Whitelisted Players [Max 3]")
sleep(1)

for i = 1,3 do
  term.setCursorPos(2,i+3)
  textutils.slowPrint(whitelist[i])
  sleep(0.2)
end

term.setCursorPos(2,8)
write("[Add/Remove] > ")
local code = read()

if code == "Add" then
  term.clear()
  term.setCursorPos(2,2)
  write("Insert Player IGN > ")
  local IGN = read()
  table.insert(whitelist,IGN)
  save(whitelist,WL)
  term.clear()
  term.setCursorPos(2,2)
  write(IGN.." has been whitelisted.")
  sleep(2)
  breakInterface()
elseif code == "Remove" then
  term.clear()
  term.setCursorPos(2,2)
  write("Whitelisted Players [Max 3]")
  sleep(1)
  for i = 1,3 do
    term.setCursorPos(2,i+3)
    textutils.slowPrint(whitelist[i].."  "..i)
    sleep(0.2)
  end
  term.setCursorPos(2,8)
  write("Insert Player Number > ")
  local IGN = read()
  table.remove(whitelist,IGN)
  save(whitelist,WL)
  term.clear()
  term.setCursorPos(2,2)
  write("Whitelisted Players [Max 3]")
  sleep(1)
  for i = 1,3 do
    term.setCursorPos(2,i+3)
    textutils.slowPrint(whitelist[i])
    sleep(0.2)
  end
  sleep(2)
  breakInterface()
  else
    term.clear()
    term.setCursorPos(10,7)
    printError("Command Not Recognised")
    sleep(1)
    breakInterface()
  end
end
-- Add Player
local function addPlayerToWhitelist(playerName)
  whitelist[playerName] = true
end
-- Check Player
local function isPlayerInWhitelist(playerName)
  return whitelist[playerName]
end
-- mainDefenceLoop Function
function mainDefenceLoop()
  for k, v in pairs(sensor.getPlayers()) do

    if isPlayerInWhitelist(v.name) then
      --parallel.waitForAny(breakInterface,mainDefenceLoop)
    else
      for i = 1,10 do
      turtle.suckUp()
      turtle.dropDown()
    end

    for i = 0,4 do
      rs.setOutput("front", true)
      sleep(0.3)
      rs.setOutput("front", false)
      sleep(6)
    end
    term.clear()
    term.setCursorPos(1,1)
    shell.run("delete ME")
    print("ItDied")
    sleep(3)
    os.shutdown()
    end
  end
end

parallel.waitForAny(breakInterface,mainDefenceLoop)
Edited on 05 September 2017 - 07:28 PM
JustIan12 #14
Posted 05 September 2017 - 10:38 PM
Thank you so much for helping me I will make sure that my code structure is less really screwed up in the future XD.

There is however an issue when the function on line 189 returns whitelist [ playerName ] when I run the code fresh a first time it works but the if statement still skips to else even though I am in proximity and whitelisted after turning the computer off and on again it loads then gives the error ME:190: attempt to index ? (a nil value)

-- Code Written by Ian
-- Before using you need to manually add and assign a table to the WL file by removing the commented line then re adding it.
-- Bootstrapper
term.clear()
term.setTextColor(colors.cyan)
term.setCursorPos(12,4)
print("M.E Defence System")
term.setTextColor(colors.blue)
term.setCursorPos(16 ,7)
print("[Continue]")
os.pullEvent("mouse_click")
term.clear()
term.setCursorPos(15,5)
textutils.slowPrint("Activating")
sleep(1)
local x = 14
while x < 25 do
  paintutils.drawPixel(x,7, colors.green)
  sleep(0.2)
  x = x + 1
end
-- Variables
sensor = peripheral.wrap("left")
-- breakInterface Function
function breakInterface()
  while true do
    term.clear()
    term.setCursorPos(12,5)
    term.setTextColor(colors.white)
    write("Defence Operational")
    term.setCursorPos(8,7)
    write("Code > ")
    local code = read()
    if code == "Safe" then
	  term.clear()
	  term.setCursorPos(10,7)
	  print("Protection Deactivated")
	  sleep(1)
	  os.shutdown()
    elseif code == "Vent" then
	  venting()
    elseif code == "List" then
	  list()
	  return
    else
	  term.clear()
	  term.setCursorPos(10,7)
	  printError("Command Not Recognised")
	  sleep(1)
	  --breakInterface()
    end
end
end
-- venting Function
function venting()
  while true do
    term.clear()
    term.setCursorPos(12,5)
    term.setTextColor(colors.white)
    write("Are you sure?")
    term.setCursorPos(8,7)
    write("[Y/N] > ")
    local code = read()
    if code == "Y" then
	  term.clear()
	  term.setCursorPos(17,5)
	  textutils.slowPrint("Venting")
	  for i = 0,4 do
	    rs.setOutput("back", true)
	    sleep(0.1)
	    rs.setOutput("back", false)
	    sleep(6)
	  end
	  term.clear()
	  term.setCursorPos(1,1)
	  shell.run("delete ME")
	  os.shutdown()
    elseif code == "N" then
	  term.clear()
	  term.setCursorPos(12,5)
	  textutils.slowPrint("Venting Stopped")
	  sleep(1)
	  return
    else
	  term.clear()
	  term.setCursorPos(10,7)
	  printError("Command Not Recognised")
	  sleep(1)
    end
  end
end
-- whitelist function
function list()
-- Save
local function save(whitelist,WL)
  local file = fs.open("WL","w")
  file.write(textutils.serialize(whitelist))
  file.close()
end
-- Load
local function pull(WL)
  local file = fs.open("WL","r")
  local data = file.readAll()
  file.close()
  return textutils.unserialize(data)
end
whitelist = {}
whitelist = pull(WL) -- A table needs to exist before using this
term.clear()
term.setCursorPos(2,2)
write("Whitelisted Players [Max 3]")
sleep(1)
for i = 1,3 do
  term.setCursorPos(2,i+3)
  textutils.slowPrint(whitelist[i])
  sleep(0.2)
end
term.setCursorPos(2,8)
write("[Add/Remove] > ")
local code = read()
if code == "Add" then
  term.clear()
  term.setCursorPos(2,2)
  write("Insert Player IGN > ")
  local IGN = read()
  table.insert(whitelist,IGN)
  save(whitelist,WL)
  term.clear()
  term.setCursorPos(2,2)
  write(IGN.." has been whitelisted.")
  sleep(2)
  breakInterface()
elseif code == "Remove" then
  term.clear()
  term.setCursorPos(2,2)
  write("Whitelisted Players [Max 3]")
  sleep(1)
  for i = 1,3 do
    term.setCursorPos(2,i+3)
    textutils.slowPrint(whitelist[i].."  "..i)
    sleep(0.2)
  end
  term.setCursorPos(2,8)
  write("Insert Player Number > ")
  local IGN = read()
  table.remove(whitelist,IGN)
  save(whitelist,WL)
  term.clear()
  term.setCursorPos(2,2)
  write("Whitelisted Players [Max 3]")
  sleep(1)
  for i = 1,3 do
    term.setCursorPos(2,i+3)
    textutils.slowPrint(whitelist[i])
    sleep(0.2)
  end
  sleep(2)
  breakInterface()
  else
    term.clear()
    term.setCursorPos(10,7)
    printError("Command Not Recognised")
    sleep(1)
    breakInterface()
  end
end
-- Add Player
local function addPlayerToWhitelist(playerName)
  whitelist[playerName] = true
end
-- Check Player
local function isPlayerInWhitelist(playerName)
  return whitelist[playerName]
end
-- mainDefenceLoop Function
function mainDefenceLoop()
  for k, v in pairs(sensor.getPlayers()) do
    if isPlayerInWhitelist(v.name) then
	  breakInterface()
    else
	  for i = 1,10 do
	  turtle.suckUp()
	  turtle.dropDown()
    end
    for i = 0,4 do
	  rs.setOutput("front", true)
	  sleep(0.3)
	  rs.setOutput("front", false)
	  sleep(6)
    end
    term.clear()
    term.setCursorPos(1,1)
    shell.run("delete ME")
    print("ItDied")
    sleep(3)
    os.shutdown()
    end
  end
end
parallel.waitForAny(breakInterface,mainDefenceLoop)
KingofGamesYami #15
Posted 05 September 2017 - 11:38 PM
I think you have your numbers mixed up a bit… the return is on line 169, and line 190 is an "end" which could hardly throw that error.

On line 126 you used table.insert to add a name to the whitelist, NOT the function I gave you (addPlayerToWhitelist). That's not going to save properly, which would explain the first problem.

As for the error, I'm not quite certain what could be causing that. Fix the issue I mentioned above, then see if it comes back… if it does, give me the line number and I'll try to track it down.

You've been doing great at giving me the information I need to solve your problems, keep it up! :)/>
JustIan12 #16
Posted 06 September 2017 - 10:38 AM
I feel like that was a tiny bit passive aggressive XD I fully understand We have been at it for almost a week now I'm sorry for throwing all these basic issues at you but thank you so much It really helps me actually learn the language. In fact I think most of my learning have been from your responses on the forum so thank you again for helping learn lua XD I will try and fix the issue now. :)/>
JustIan12 #17
Posted 06 September 2017 - 10:59 AM
Yeah I did what you told me to do, I am using notepad++ to edit this. Here it says that on line 190

ME:190: attempt to index ? (a nil value)

http://prntscr.com/ghq97d

when I disable that line I and try to add players to the list I get another error this is after I called your function

Edit: Its because I tried to call before declaring I just saw that now as I was typing.

Edit x2: Nope http://prntscr.com/ghqba4 http://prntscr.com/ghqblh and the old error still persists its just on a different line because I moved the functions to the top http://prntscr.com/ghqcaz.

Edit x3: the error with the return has dissipated after running the code once after disabling the line then enabling it again when I run List then try to add a player I get this http://prntscr.com/ghqeko

-- Code Written by Ian
-- Before using you need to manually add and assign a table to the WL file by removing the commented line then re adding it.
-- Bootstrapper
term.clear()
term.setTextColor(colors.cyan)
term.setCursorPos(12,4)
print("M.E Defence System")
term.setTextColor(colors.blue)
term.setCursorPos(16 ,7)
print("[Continue]")
os.pullEvent("mouse_click")
term.clear()
term.setCursorPos(15,5)
textutils.slowPrint("Activating")
sleep(1)
local x = 14
while x < 25 do
  paintutils.drawPixel(x,7, colors.green)
  sleep(0.2)
  x = x + 1
end
-- Variables
sensor = peripheral.wrap("left")
-- breakInterface Function
function breakInterface()
  while true do
    term.clear()
    term.setCursorPos(12,5)
    term.setTextColor(colors.white)
    write("Defence Operational")
    term.setCursorPos(8,7)
    write("Code > ")
    local code = read()
    if code == "Safe" then
	  term.clear()
	  term.setCursorPos(10,7)
	  print("Protection Deactivated")
	  sleep(1)
	  os.shutdown()
    elseif code == "Vent" then
	  venting()
    elseif code == "List" then
	  list()
	  return
    else
	  term.clear()
	  term.setCursorPos(10,7)
	  printError("Command Not Recognised")
	  sleep(1)
	  --breakInterface()
    end
end
end
-- Add Player
local function addPlayerToWhitelist(playerName)
  whitelist[playerName] = true
end
-- Check Player
local function isPlayerInWhitelist(playerName)
  return whitelist[playerName]
end
-- venting Function
function venting()
  while true do
    term.clear()
    term.setCursorPos(12,5)
    term.setTextColor(colors.white)
    write("Are you sure?")
    term.setCursorPos(8,7)
    write("[Y/N] > ")
    local code = read()
    if code == "Y" then
	  term.clear()
	  term.setCursorPos(17,5)
	  textutils.slowPrint("Venting")
	  for i = 0,4 do
	    rs.setOutput("back", true)
	    sleep(0.1)
	    rs.setOutput("back", false)
	    sleep(6)
	  end
	  term.clear()
	  term.setCursorPos(1,1)
	  shell.run("delete ME")
	  os.shutdown()
    elseif code == "N" then
	  term.clear()
	  term.setCursorPos(12,5)
	  textutils.slowPrint("Venting Stopped")
	  sleep(1)
	  return
    else
	  term.clear()
	  term.setCursorPos(10,7)
	  printError("Command Not Recognised")
	  sleep(1)
    end
  end
end
-- whitelist function
function list()
-- Save
local function save(whitelist,WL)
  local file = fs.open("WL","w")
  file.write(textutils.serialize(whitelist))
  file.close()
end
-- Load
local function pull(WL)
  local file = fs.open("WL","r")
  local data = file.readAll()
  file.close()
  return textutils.unserialize(data)
end
whitelist = {}
whitelist = pull(WL) -- A table needs to exist before using this
term.clear()
term.setCursorPos(2,2)
write("Whitelisted Players [Max 3]")
sleep(1)
for i = 1,3 do
  term.setCursorPos(2,i+3)
  textutils.slowPrint(whitelist[i])
  sleep(0.2)
end
term.setCursorPos(2,8)
write("[Add/Remove] > ")
local code = read()
if code == "Add" then
  term.clear()
  term.setCursorPos(2,2)
  write("Insert Player IGN > ")
  local IGN = read()
  table.insert(addPlayerToWhitelist,IGN)
  save(whitelist,WL)
  term.clear()
  term.setCursorPos(2,2)
  write(IGN.." has been whitelisted.")
  sleep(2)
  breakInterface()
elseif code == "Remove" then
  term.clear()
  term.setCursorPos(2,2)
  write("Whitelisted Players [Max 3]")
  sleep(1)
  for i = 1,3 do
    term.setCursorPos(2,i+3)
    textutils.slowPrint(whitelist[i].."  "..i)
    sleep(0.2)
  end
  term.setCursorPos(2,8)
  write("Insert Player Number > ")
  local IGN = read()
  table.remove(whitelist,IGN)
  save(whitelist,WL)
  term.clear()
  term.setCursorPos(2,2)
  write("Whitelisted Players [Max 3]")
  sleep(1)
  for i = 1,3 do
    term.setCursorPos(2,i+3)
    textutils.slowPrint(whitelist[i])
    sleep(0.2)
  end
  sleep(2)
  breakInterface()
  else
    term.clear()
    term.setCursorPos(10,7)
    printError("Command Not Recognised")
    sleep(1)
    breakInterface()
  end
end
-- mainDefenceLoop Function
function mainDefenceLoop()
  for k, v in pairs(sensor.getPlayers()) do
    if isPlayerInWhitelist(v.name) then
	  breakInterface()
    else
	  for i = 1,10 do
	  turtle.suckUp()
	  turtle.dropDown()
    end
    for i = 0,4 do
	  rs.setOutput("front", true)
	  sleep(0.3)
	  rs.setOutput("front", false)
	  sleep(6)
    end
    term.clear()
    term.setCursorPos(1,1)
    shell.run("delete ME")
    print("ItDied")
    sleep(3)
    os.shutdown()
    end
  end
end
parallel.waitForAny(breakInterface,mainDefenceLoop)
KingofGamesYami #18
Posted 06 September 2017 - 02:50 PM
This line number difference has me very confused, I'm using Sublime Text 3 myself, and according to it your code is only 200 lines long.

Are you sure this is the full code? I don't see how an editor would make a difference.

Regardless, you're still not using my whitelist table format correctly - table.insert and table.remove have no place here.

Examples:

Adding a player to the whitelist

whitelist[ "KingofGamesYami" ] = true

Removing a player to the whitelist

whitelist[ "KingofGamesYami" ] = nil

Checking if a player is whitelisted:

if whitelist[ "KingofGamesYami" ] then


The error on line 152 (134 for me) is fairly self-explanatory. "Table expected" means table.insert wanted a table (eg whitelist), "got function" means you gave it a function (eg. addPlayerToWhitelist). You don't need table.insert at all there, instead you should do:

addTableToWhitelist( IGN )

If you could provide a comment ( start with –# ) on the lines mentioned in error messages, that would help me a lot since there seems to be a mismatch.

For example,


...
  term.clear()
  term.setCursorPos(2,2)
  write("Insert Player IGN > ")
  local IGN = read()
  table.insert(addPlayerToWhitelist,IGN) --# line 152
  save(whitelist,WL)
  term.clear()
  term.setCursorPos(2,2)
  write(IGN.." has been whitelisted.")
  sleep(2)
...
JustIan12 #19
Posted 06 September 2017 - 05:09 PM
Okay thank you so much :)/> I will go ahead and do that now.
JustIan12 #20
Posted 06 September 2017 - 05:38 PM
I am just going to come back at a later date and rewrite the entire code because I am getting frustrated with the errors, And sorry for wasting your time.

Thanks for all the help!

- Ian
Edited on 06 September 2017 - 03:40 PM
JustIan12 #21
Posted 15 February 2018 - 12:22 PM
Oh heiiii I'm back to try again.

I have gone through and rewrote the entire code adding and removing sections that were causing issues and I have come to this. I have read all of the comments made previously and tried to integrate them. I have fixed the majority of errors. However, when I run the list command.

-- List Function [INCOMPLETE]
function list()
	term.clear()
	term.setCursorPos(2,2)
	write("Whitelisted Players [Max 3]")
	sleep(1)
	for k, v in pairs(whitelist) do
		term.setCursorPos(2,4)
		textutils.slowPrint(v)
		sleep(0.5)
	end
	term.setCursorPos(2,8)
	write("[Add/Remove] > ")
	local code = read()

	if code == "Add" then
		term.clear()
		term.setCursorPos(2,2)
		write("Insert Player IGN > ")
		local IGN = read()
		addPlayerToWhitelist(IGN)
		term.clear()
		term.setCursorPos(2,2)
		write(IGN.." has been whitelisted.")
		sleep(2)
		userInterface()
  
	elseif code == "Remove" then
		term.clear()
		term.setCursorPos(2,2)
		write("Whitelisted Players")
		sleep(1)
		for k, v in pairs(whitelist) do
			term.setCursorPos(2,4)
			textutils.slowPrint(v)
			sleep(0.5)
		end
	  
		term.setCursorPos(2,4)
		write("Insert Player IGN > ")
		local IGN = read()
		removePlayerToWhitelist(IGN)
		term.clear()
		term.setCursorPos(2,2)
		write(IGN.." has been removed from the whitelist.")
		sleep(2)
		userInterface()
	else
		term.clear()
		term.setCursorPos(10,7)
		printError("Command Not Recognised")
		sleep(1)
		userInterface()
	end
end
whitelist = {"JustIan12"}
userInterface()

It prints whats in the table whitelist which is fine. But, when I go to add a player then check to see if whitelist has been updated it overlaps with "true" on the same line which I also tried to fix but I would get the error that I could not add numbers to strings. The only reasoning is that the function you gave me actually allocated the string true to the table or I am using it incorrectly again :P/>

I tried to remove true but nothing happened it just continued printing true over my name.

http://prntscr.com/if2tte

-- Add Player to internal table [COMPLETE]
function addPlayerToWhitelist(IGN)
	whitelist[IGN] = true
end
-- Remove Player from internal table [COMPLETE]
function removePlayerToWhitelist(IGN)
	whitelist[IGN] = nil
end
-- Check Player idk what this does [COMPLETE]
function isPlayerInWhitelist(IGN)
	return whitelist[IGN]
end

After fixing the list table thing I was going to implement the iteration loop

-- Main Defence Loop
for k, v in pairs(sensor.getPlayers()) do --# iterate through the "tNames" table
  if( whitelist[ v ] ) then --# this is where the structure I showed before is handy; I can simply index the 't' table with a name to tell if it's whitelisted
	print("Regognised Player")
  else
	print("Not Recognised Player")
  end
end

I hope that is correct.

And lastly here is the entire code for refrence.


-- Code Written by Ian
-- Before using you need to manually add and assign a table to the WL file by removing the commented line then re adding it.

-- Bootstrapper [COMPLETE]
term.clear()
term.setTextColor(colors.cyan)
term.setCursorPos(12,4)
print("M.E Defence System")
term.setTextColor(colors.blue)
term.setCursorPos(16 ,7)
print("[Continue]")

os.pullEvent("mouse_click")term.clear()
term.setCursorPos(3,3)
write("What side is the sensor on? : ")
pSide = read()
term.clear()
term.setCursorPos(15,5)
textutils.slowPrint("Activating")
sleep(1)
local x = 14
while x < 25 do
  paintutils.drawPixel(x,7, colors.green)
  sleep(0.2)
  x = x + 1
endsensor = peripheral.wrap(pSide)
-- User Interface [COMPLETE]function userInterface()
	while true do
	  term.clear()
	  term.setCursorPos(12,5)
	  term.setTextColor(colors.white)
	  write("Defence Operational")
	  term.setCursorPos(8,7)
	  write("Code > ")
	  local code = read()
	  if code == "Safe" then
		term.clear()
		term.setCursorPos(10,7)
		print("Protection Deactivated")
		sleep(1)
		os.shutdown()
	  elseif code == "Vent" then
		venting()
	  elseif code == "List" then
		list()
		return
	  else
		term.clear()
		term.setCursorPos(10,7)
		printError("Command Not Recognised")
		sleep(1)
		userInterface()
	  end
  end
end
-- Vent Function [COMPLETE]function venting()
	while true do
	  term.clear()
	  term.setCursorPos(12,5)
	  term.setTextColor(colors.white)
	  write("Are you sure?")
	  term.setCursorPos(8,7)
	  write("[Y/N] > ")
	  local code = read()
	  if code == "Y" then
		term.clear()
		term.setCursorPos(17,5)
		textutils.slowPrint("Venting")
		for i = 0,4 do
		  rs.setOutput("back", true)
		  sleep(0.1)
		  rs.setOutput("back", false)
		  sleep(6)
		end
		term.clear()
		term.setCursorPos(1,1)
		shell.run("delete ME")
		os.shutdown()
	  elseif code == "N" then
		term.clear()
		term.setCursorPos(12,5)
		textutils.slowPrint("Venting Stopped")
		sleep(1)
		return
	  else
		term.clear()
		term.setCursorPos(10,7)
		printError("Command Not Recognised")
		sleep(1)
	  end
	end
end
-- Save table to file / RAM [COMPLETE]function save(whitelist,SWL)
	local file = fs.open("SWL","w")
	file.write(textutils.serialize(whitelist))
	file.close()
end
	
-- Load from file to table / RAM [COMPLETE]
	
function pull(SWL)
	local file = fs.open("SWL","r")
	local data = file.readAll()
	file.close()
	return textutils.unserialize(data)
end
-- Add Player to internal table [COMPLETE]function addPlayerToWhitelist(IGN)
	whitelist[IGN] = true
end
-- Remove Player from internal table [COMPLETE]function removePlayerToWhitelist(IGN)
	whitelist[IGN] = nil
end
-- Check Player idk what this does [COMPLETE]function isPlayerInWhitelist(IGN)
	return whitelist[IGN]
end
-- List Function [INCOMPLETE]function list()
	term.clear()
	term.setCursorPos(2,2)
	write("Whitelisted Players [Max 3]")
	sleep(1)
	for k, v in pairs(whitelist) do
		term.setCursorPos(2,4)
		textutils.slowPrint(v)
		sleep(0.5)
	end	term.setCursorPos(2,8)
	write("[Add/Remove] > ")
	local code = read()

	if code == "Add" then
		term.clear()
		term.setCursorPos(2,2)
		write("Insert Player IGN > ")
		local IGN = read()
		addPlayerToWhitelist(IGN)
		term.clear()
		term.setCursorPos(2,2)
		write(IGN.." has been whitelisted.")
		sleep(2)
		userInterface()
  
	elseif code == "Remove" then
		term.clear()
		term.setCursorPos(2,2)
		write("Whitelisted Players")
		sleep(1)
		for k, v in pairs(whitelist) do
			term.setCursorPos(2,4)
			textutils.slowPrint(v)
			sleep(0.5)
		end
	  
		term.setCursorPos(2,4)
		write("Insert Player IGN > ")
		local IGN = read()
		removePlayerToWhitelist(IGN)
		term.clear()
		term.setCursorPos(2,2)
		write(IGN.." has been removed from the whitelist.")
		sleep(2)
		userInterface()	else
		term.clear()
		term.setCursorPos(10,7)
		printError("Command Not Recognised")
		sleep(1)
		userInterface()
	end
end
whitelist = {"JustIan12"}
userInterface()

Thanks!

- Ian

Edit: I think the reason why there was a mismatch was because of the code moving up a line when I make comments.
Edited on 15 February 2018 - 11:25 AM
killzoms #22
Posted 15 February 2018 - 08:48 PM
Hi guys I helped JustIan with his issue
JustIan12 #23
Posted 15 February 2018 - 10:12 PM
I DID IT!