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