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

<[SOLVED]>

Started by Famous5000, 03 January 2016 - 03:49 PM
Famous5000 #1
Posted 03 January 2016 - 04:49 PM
I am having a problem with my program….
Whenever I try to access an index on it it just returns (ERROR: Index expected, got string)
I'll give a snippet of the code and if I need to post more of it tell me and I will add it.
Whitelist[50] = P
where P is the player to be added and Whitelist is the list of allowed players.
I've tried EVERY single value, it just will not work! :(/>
EDIT: Here's my code:
Spoiler

local P = peripheral.find("modem")
if P == nil then
  error("There is no router on this PC!")
end
print("Got the modem!")
P.open(15)
Whitelist = {"Famous5000"}
if fs.exists("Whitelist") then
  Whitelist = io.open("Whitelist","r")
  Whitelist = Whitelist:read()
  local W = {}
  for I = 1,#Whitelist do
	W = Whitelist[I] -- I screwed up here, should have been W[I] = Whitelist[I] <FIXED>
  end
  Whitelist = W
end
while true do
  local _,_,Channel,_,Message = os.pullEvent("modem_message")
  if type(Message) == "table" then
	sleep(0.1)
	local Plr = Message[1]
	INVALID = false
	if Plr ~= "Famous5000" then
	  INVALID = true
	  for I = 1,#Whitelist do
		if Plr == Whitelist[I] then
		  INVALID = false
		end
	  end
	  if INVALID == true then
		term.setTextColor(colors.red)
		print("A blocked attempt to access this system from "..Plr)
		term.setTextColor(colors.white)
	  end
	end
	local Msg = Message[2]
	if INVALID == false then
	  if Msg:lower() == "open" then
		redstone.setAnalogOutput("top",15)
		sleep(2)
		redstone.setAnalogOutput("top",0)
	  end
	  if Msg:lower() == "shutdown" then
		break
	  end
	  if Msg:lower():sub(0,3) == "add" then
		local P = Msg:sub(5)
		local C = 0
		for I = 1,#Whitelist do
		  C = I + 1
		end
		C = tonumber(C)
		Whitelist[1] = P --Problem resides here, prints error
	  end
	  if Msg:lower():sub(0,3) == "rem" then	
		local P = Msg:sub(5)
		if P == "clr" then
		  Whitelist = {"Famous5000"}
		else
		  for I = 1,#Whitelist do
			if Whitelist[I]:lower():match(P:lower()) then
			  table.remove(Whitelist,I)
			end
		  end
		end
	  end
	  print("Recieved message from "..Plr)
	end
  end
end
local W = fs.open("Whitelist","w")
print("I have been told to shut down. Rebooting...")
sleep(3)
if W then
  for n,T in ipairs(Whitelist) do
	W.write(T.."\n")
  end
  W.close()
else
  print("Access denied. Please fix this")
end
sleep(10)
if not fs.exists("startup") then
  local R = fs.open("startup","w")
  Lines = {"shell.run(\"Detector\")",
  "os.reboot()"}
  if R then
	for n,T in ipairs(Lines) do
	  R.write(T.."\n")
	end
  end
  R.close()
  os.reboot()
end
Oops! I see an obvious problem. Going to try to solve it via the computercraft thing and if that doesn't work, I'll report back to you.
Thanks for the help.
That error message means Whitelist is a string. I don't know how or why until you post the code, but it is.
I completely spaced out about adding the . It works fine now.
Now time to add the final thing, and if that works, you won't hear from me again on this post.
SpoilerPS, it's my first time using this thread, so now I know to paste the whole code next time. Thanks for the help!
Now I'm having a problem with my Wifi Controller (see below's spoiler). It says on line 5 that it's attempting to call nil.
Spoiler
print("Please input username.")
local U = io.input() --It's supposed to wait until someone inputs something here, but that's not happening, so I'm confused
local M = peripheral.find("modem")
M.open(15)
M.broadcast(15,15,{U,"TEST"})
local _,_,_,_,Msg = os.pullEvent("modem_message")
if Msg == "Test Successful!" then
  term.setTextColor(colors.green)
  print("Successfully validated!")
  term.setTextColor(colors.white)
end
while true do
  term.clear()
  print("Input the command.")
  local C = io.input()
  M.broadcast(15,15,{U,C})
end
EDIT2: I'm dumb. Fixed the errors and am waiting for thread to close.
Edited on 03 January 2016 - 11:17 PM
Lyqyd #2
Posted 03 January 2016 - 07:24 PM
Please post the rest of the code.
Creator #3
Posted 03 January 2016 - 07:43 PM
If you want to add players to a whitelist, make this: players[p] = true.

And post the whole code.
KingofGamesYami #4
Posted 03 January 2016 - 07:49 PM
That error message means Whitelist is a string. I don't know how or why until you post the code, but it is.
Famous5000 #5
Posted 03 January 2016 - 11:06 PM
Oh, OOPS. I'm posting the rest of my code right now.
www.pastebin.com/zJDwGDq0
If you can't get it from there, I have it on the spoiler below.
Spoiler

local P = peripheral.find("modem")
if P == nil then
  error("There is no router on this PC!")
end
print("Got the modem!")
P.open(15)
Whitelist = {"Famous5000"}
if fs.exists("Whitelist") then
  Whitelist = io.open("Whitelist","r")
  Whitelist = Whitelist:read()
  local W = {}
  for I = 1,#Whitelist do
	W = Whitelist[I]
  end
  Whitelist = W
end
while true do
  local _,_,Channel,_,Message = os.pullEvent("modem_message")
  if type(Message) == "table" then
	sleep(0.1)
	local Plr = Message[1]
	INVALID = false
	if Plr ~= "Famous5000" then
	  INVALID = true
	  for I = 1,#Whitelist do
		if Plr == Whitelist[I] then
		  INVALID = false
		end
	  end
	  if INVALID == true then
		term.setTextColor(colors.red)
		print("A blocked attempt to access this system from "..Plr)
		term.setTextColor(colors.white)
	  end
	end
	local Msg = Message[2]
	if INVALID == false then
	  if Msg:lower() == "open" then
		redstone.setAnalogOutput("top",15)
		sleep(2)
		redstone.setAnalogOutput("top",0)
	  end
	  if Msg:lower() == "shutdown" then
		break
	  end
	  if Msg:lower():sub(0,3) == "add" then
		local P = Msg:sub(5)
		local C = 0
		for I = 1,#Whitelist do
		  C = I + 1
		end
		C = tonumber(C)
		Whitelist[1] = P
	  end
	  if Msg:lower():sub(0,3) == "rem" then	
		local P = Msg:sub(5)
		if P == "clr" then
		  Whitelist = {"Famous5000"}
		else
		  for I = 1,#Whitelist do
			if Whitelist[I]:lower():match(P:lower()) then
			  table.remove(Whitelist,I)
			end
		  end
		end
	  end
	  print("Recieved message from "..Plr)
	end
  end
end
local W = fs.open("Whitelist","w")
print("I have been told to shut down. Rebooting...")
sleep(3)
if W then
  for n,T in ipairs(Whitelist) do
	W.write(T.."\n")
  end
  W.close()
else
  print("Access denied. Please fix this")
end
sleep(10)
if not fs.exists("startup") then
  local R = fs.open("startup","w")
  Lines = {"shell.run(\"Detector\")",
  "os.reboot()"}
  if R then
	for n,T in ipairs(Lines) do
	  R.write(T.."\n")
	end
  end
  R.close()
  os.reboot()
end

I'm new to Computercraft Lua, BUT I have a lot of experience coding in ROBLOX Lua. So if that influences any of the replies, well, it's there.
Thanks to whoever merged the 2 comments together.
Edited on 03 January 2016 - 11:29 PM
KingofGamesYami #6
Posted 04 January 2016 - 12:10 AM
1) You didn't close your file handle opened on line 9

2) io.read returns a string, not a table. Even if the file contains a file. You'll want to use textutils.unserialize.
Famous5000 #7
Posted 04 January 2016 - 12:28 AM
1) You didn't close your file handle opened on line 9

2) io.read returns a string, not a table. Even if the file contains a file. You'll want to use textutils.unserialize.
1. It worked OK even without closing it (AFTER edits)
2. Aparantly it returns a table because the whitelist system was working while using that function..?