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

Chat program, help with error

Started by popdog15, 05 February 2013 - 08:09 AM
popdog15 #1
Posted 05 February 2013 - 09:09 AM
I'm trying to make a chat program, and right now, I'm making the clientside version. Due to that, the rest of the specifics aren't needed. There is an error on line 22 that I can't figure out, I thought it might be that I used the variable cName in a way that it shouldn't, but I changed it. The error is "expected string" on line 22.


-- test

if firstStart == true then
  setClientName()
else
  -- to be chat client
function setClientName()
  
  write"Insert client name:  "
  name = read()
   print("Thank you"..name.."!")
   name = clientName
	 hWrite = fs.open(cName, "w")
hWrite.write(clientName)
  hWrite.close()
    firstStart = false
  
  
end
if fs.exists(cName) then
  local hRead = assert(fs.open(cname, "r"))
   cName = hRead.readLine()
    hRead.close()
  print("Hello "..clientName.."!")
end
end
 
GopherAtl #2
Posted 05 February 2013 - 09:20 AM
name=clientName
hWrite=fs.open(cName,"w")

Just guessing, as you don't give us complete code and we have no idea which is line #22. You seem to be using a lot of different variables to hold the same value for some reason; I'd recommend cleaning that up, and consistently using just one.
popdog15 #3
Posted 05 February 2013 - 09:23 AM
Okay- I edited the code some, but now, it does nothing when I run the program. (Also- that is the current complete code.)


-- test

if firstStart == true then
  setClientName()
else
  -- to be chat client
function setClientName()
  
  write"Insert client name:  "
  name = read()
   print("Thank you"..name.."!")
   name=clientName
	 hWrite = fs.open("Clientname", "w")
hWrite.write(clientName)
  hWrite.close()
    firstStart = false
  
  
end
if fs.exists("Clientname") then
  local hRead = assert(fs.open("clientname", "r"))
   cName = hRead.readLine()
    hRead.close()
  print("Hello "..clientName.."!")
end
end
 
GopherAtl #4
Posted 05 February 2013 - 09:25 AM
:sigh:

I didn't post a fix.

I posted two lines where you assign a variable, and then use a completely different variable. What is cName, and where is it defined? it's nowhere in this code.

Post the full code, on pastebin where we can get line numbers, not just the part you think is relevant.
PixelToast #5
Posted 05 February 2013 - 09:26 AM
-snip-
popdog15 #6
Posted 05 February 2013 - 10:17 AM
As I said- this is the entire code.

http://pastebin.com/vb0pXymM
PixelToast #7
Posted 05 February 2013 - 10:29 AM
line 12: name=clientName
clientName hasnt been defined
line 21: local hRead = assert(fs.open("clientname", "r"))
your using assert, why?
popdog15 #8
Posted 05 February 2013 - 10:35 AM
line 12: name=clientName
clientName hasnt been defined
line 21: local hRead = assert(fs.open("clientname", "r"))
your using assert, why?

Honestly, I'm not sure anymore. It seemed like a good idea at the time.
remiX #9
Posted 05 February 2013 - 10:56 PM
Okay- I edited the code some, but now, it does nothing when I run the program. (Also- that is the current complete code.)


-- test

if firstStart == true then
  setClientName()
else
  -- to be chat client
function setClientName()
  
  write"Insert client name:  "
  name = read()
   print("Thank you"..name.."!")
   name=clientName
     hWrite = fs.open("Clientname&quotu;, "w")
hWrite.write(clientName)
  hWrite.close()
    firstStart = false
  
  
end
if fs.exists("Clientname") then
  local hRead = assert(fs.open("clientname", "r"))
   cName = hRead.readLine()
    hRead.close()
  print("Hello "..clientName.."!")
end
end
 

Where do you set firstStart? It should error as you initialize the program because it's nil.If you're trying to make the varuable clientName equal name then change it around:clientName = name
popdog15 #10
Posted 06 February 2013 - 03:06 AM
Thanks to those who helped- I got it working, or, I got the first part of the client working.