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

unexpected symbol table, no clue

Started by grimofdoom, 21 December 2014 - 05:44 AM
grimofdoom #1
Posted 21 December 2014 - 06:44 AM
i have spent abut 2 hours trying to get this right, on line 30, i always keep getting the error of unexpected symbol. What I am attempting to do is add a key with data adding table.insert. This is the base of a chat server I am attempting to set up the chat server for some friends(to show off a bit too).


rednet.open("right")

users={admin=1,apple=1}--ID of all members on the server
--------------functions--------
function displayusers()
  for i=1,#users do
    print ("ID:"..users[i][1].."     nick:"..users[i][2])
  end
end
function receive()
  while true do
    id,msg=rednet.receive()
    decide(id,msg)
  end
end

function send(msg,id)
  for i in users() do
  end
end

function decide(id,msg,users)--deciding what each message is
  i=1
  command={}
  for word in msg:gmatch("%a+") do
    command[i]=word
    i=i+1
  end
  if command[1]=="register" then--if register works
    table.insert(users,[#users+1],command[2]=id)
    rednet.send(id,"welcome,"..command[2].."!")
    print(id.." has registered as "..command[2])
    displayusers()
  else--                   if register did not work
  print("not registered")
    rednet.send(id,"your registery has not gone through")
    print(id.." received an error")
    print("[message]"..msg)
  end
end
--main function
function status()
  term.clear()
  term.setCursorPos(1,1)
  print("server is running...version A.1.01")
  displayusers()
end
----------running--------
status()
receive()
(sorry if it is a bit cluttered, i have only been using computercraft about 6 days and am a bit new to it still in syntax)
Bomb Bloke #2
Posted 21 December 2014 - 07:13 AM
table.insert(users,[#users+1],command[2]=id)

"command[2]=id" isn't a "value" you can pass to table.insert(); it's an instruction requesting that command[2] be set to the value of id. Lua's wondering why you've got a lone = symbol there.

It seems what you wanted to do was:

users[id] = command[2]

Note that this will lead to values being set to rather arbitrary indexes within your users table. To cut a long story short, your current displayusers() function wouldn't deal with this as-is (because #users wouldn't correctly return the number of values in a table set out this way), but could be re-written to use pairs to handle it:

local function displayusers()
	for id, nick in pairs(users) do
		print ("ID:"..id.."     nick:"..nick)
	end
end
grimofdoom #3
Posted 21 December 2014 - 07:26 AM
so i switched table.insert(users,[#users+1],command[2]=id for users[id]=command[2] and then adjusted the displayusers() accordingly…when i run it, i get an error from the same line saying expected index got nil instead… i did a print of the "id" and "command[2]" before hand to see if there is some problem with it, but it prints out as expected. (this one line has been the biggest problem…ive been stuck on it with errors for about 4 hours now,with many different combinations.)The error happends after it actually receives data
Bomb Bloke #4
Posted 21 December 2014 - 07:43 AM
You overwrote the original "users" table with a version local to the "decide" function here:

function decide(id,msg,users)

Because you didn't pass any value to fill that local version when you called the function, it ended up as nil. It's no longer a table you can index into.

Simple fix should be:

function decide(id,msg)
grimofdoom #5
Posted 21 December 2014 - 08:04 AM
It works! thank you! I cannot believe that one line was the problem… i originally added users to the line because i did not think it would use it without it.