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

Server:102: Attempt to index ? (a nil value)

Started by Emma, 28 June 2013 - 11:27 AM
Emma #1
Posted 28 June 2013 - 01:27 PM
Hi I am trying to write a chat program for a friend and am getting this error: "server:102: attempt to index ? (a nil value)"



--[[Dex chat |server| script writted by Bryan B aka incinirate.
feel free to edit as much as you want, just give me credit please.
Thanks for using DexChat TypeB V1]]

modem="back"
rednet.open(modem)
computers={}
users={}
deny=false
currtext=""

function isConnected(im)
  for i=1,#computers do
	if computers[i]==im then
	  return true
	end
  end
  return false
end


main=function()
  id,message=rednet.receive()
  print("Message from "..id..". Message: "..message)
  iter = string.gmatch(message, "%P+")
  u = iter()
  k = iter()
  if k~=nil then print("Found as, "..u.." and "..k) end
  if k=="Connecting" then
	for i=1,#computers do
	  if id==computers[i] then
		deny=true
	  end
	  if u==users[i] then
		deny=true
	  end
	end
	if deny==true then sleep(0.25) print("Sending: Denied To "..id)rednet.send(id,"Denied")
	else
	  print(u.." is connecting")
	  table.insert(computers,id)
	  table.insert(users,u)
	  for i=1,#computers do
		sleep(0.25) print("Sending: "..u.."has connected! To all computers") rednet.send(computers[i],u.." has connected!")
	  end
	end
   else if k=="disconnect" then


print(u)
		for i=1,#computers do
				print(u)
				if u~=computers[i] then print(u.."~="..computers[i]) end
print("computer "..i..":"..computers[i])
				print("user "..i..":"..users[i])
if tonumber(u)==tonumber(computers[i]) then disser=i end
end
if disser ~= nil then
				print("Disser is not NIL")
				print(disser)
print("Disconnecting "..users[disser])
for j=1,#computers do
rednet.send(computers[j],users[disser].." has disconnected.")
end
table.remove(computers,disser)
table.remove(users,disser)
end


  else
	if isConnected(id) then
	  for i=1,#computers do
		print("Sending: "..message.." to all computers.")
		rednet.send(computers[i],message)
	  end
	else
	  rednet.send(id,"You are not connected.")
	end
  end
end
end


readtxt = function()
  write(currtext)
  while true do
	e,par1 = os.pullEvent()
	if e == "char" then
	  currtext=currtext..par1
	  write(par1)
	end
	if e == "key" then
	  if par1==28 then
		break
	  end
	end
  end
end

function activateCommand(com)
  args = {}
  for arg in com:gMatch("%w+") do
  table.insert(args,arg)
  end

  if args[1] == "/kick" then
	if args[2] ~= nil then
	  for i=1,#users do
		if users[i] == args[2] then
		  foundu=true
		  if args[3] == nil then
		  rednet.send(computers[i],"You were kicked.")
		  else
		  rednet.send(computers[i],"You were kicked for "..args[3])
		  end
		end
	  end
	  if foundu == false then
		print("Did not find a user by the name of "..args[2])
		print("Usage: /kick <username> [reason]")
	  end
	end
  else if args[1] == "/help" then
	print("/help -View Commands")
	print("/kick <username> [reason] -Kicks a user")
  else
	print("Command not found. Try /help")
  end
end
end

while true do
  parallel.waitForAny(main,readtxt)
  activateCommand(got)
  sleep(0.1)
end
HurricaneCoder #2
Posted 28 June 2013 - 02:32 PM
First of all, can you define what is got in activateCommand(got) and second to separate string word by word it is


local string = "test1 test2 test3"
local args = {}
for arg in string.gmatch(string,"%w+") do --so you define the string that will be separating in the argument.
table.insert(args,arg)
end
print(args[1]) --> test1
print(args[2]) --> test2
--and so on
Emma #3
Posted 28 June 2013 - 03:10 PM
First of all, can you define what is got in activateCommand(got) and second to separate string word by word it is


local string = "test1 test2 test3"
local args = {}
for arg in string.gmatch(string,"%w+") do --so you define the string that will be separating in the argument.
table.insert(args,arg)
end
print(args[1]) --> test1
print(args[2]) --> test2
--and so on

Thanks, implemeted that idea and it works now! Thanks!
H4X0RZ #4
Posted 28 June 2013 - 07:05 PM
First of all, can you define what is got in activateCommand(got) and second to separate string word by word it is


local string = "test1 test2 test3"
local args = {}
for arg in string.gmatch(string,"%w+") do --so you define the string that will be separating in the argument.
table.insert(args,arg)
end
print(args[1]) --> test1
print(args[2]) --> test2
--and so on
That works?
I've learned it so:

local function seperate(_string)
  local tArgs = {}
  for match in _string:gmatch("[^\t]+") do
    table.insert(tArgs, match)
  end
  if tArgs[1] then
    return tArgs
  else
    return false
  end
end

local seperatedString = seperate("test1 test2 test3")
if seperatedString the
  for k,v in pairs(seperatedString) do
    print(v)
  end
else
  error("Invalid input!",0)
end
Apfeldstrudel #5
Posted 04 July 2013 - 05:59 AM
Bump, friend got this error 2 and ut seems to work sometimes
theoriginalbit #6
Posted 04 July 2013 - 06:13 AM
Bump, friend got this error 2 and ut seems to work sometimes
Based on the suggested fix (via word, not the code) from HurricaneCoder the code would no longer get this error… ever. If your problem is related specifically to the code posted in the OP please post the code that you or your friend has so that we can continue to help…. If however your error is not from the code posted in the OP please do not hijack threads and instead post a new thread containing your code and the precise error that you're getting so we can further help.