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

"Index expected, got string" when trying to set a variable in a table

Started by KeyFrame, 06 June 2014 - 10:23 AM
KeyFrame #1
Posted 06 June 2014 - 12:23 PM
Im making a program to monitor my energy storage and i am having a problem that i cannot figure out. I am trying to set a variable in a table but it tells me "Index expected, got string" and i dont know why. I have used the very same method of setting variables in tables many times before and never gotten this error.I have 2 computer in this setup. The purpous of the program i am having trouble whit is to handle commands sent to it and relay them to the main computer as the main computer is bussy getting information about the storage units.

It gives me the error at line 51, 53, 55, 57 or 59 depending on what message i pass to it through the chat_command feature in openPeripheral.

This is the effected part of the code.

elseif e == "chat_command" then
  print (var1)
  if var2 == playername then
   if var1 == "off" then
	setts["state"] = false
   elseif var1 == "on" then
	setts["state"] = true
   elseif var1 == "eu" then
	setts["display"] = "eu"
   elseif var1 == "mj" then
	setts["display"] = "mj"
   elseif var1 == "back" then
	setts["display"] = "all"
   end
  end
The setts table is called at the top of the program

Whole program: http://pastebin.com/wbWceT56
SquidDev #2
Posted 06 June 2014 - 12:30 PM
Have you tried doing setts.display = "all" or instead? Also, define variables and functions with the local keyword to prevent polluting the global scope.

I can't replicate this issue, have you tried restarting (Ctrl+R) the computer?
KeyFrame #3
Posted 06 June 2014 - 12:39 PM
Restarting did help for some reason. Thank you

Edit: Im stupid. Forgot to change playername to my username. still got the same problem :/
Edited on 06 June 2014 - 10:42 AM
KeyFrame #4
Posted 06 June 2014 - 12:45 PM
Should mention that i am on FTB Unleashed V1.1.3 whit Computercraft version 1.53
Bomb Bloke #5
Posted 06 June 2014 - 01:08 PM
I've got a hunch it's because it thinks you're trying to index into a string - didn't know you can even do that, but whatever.

You can't read/write tables directly into files. Take a look into textutils.serialize() and textutils.unserialize().
KeyFrame #6
Posted 06 June 2014 - 01:16 PM
You are spot on there Bomb Bloke. forgot to serialize and unserialize my setting when i saved and loaded them from the files. Thank you!