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

Attempt to call string im going to destroy my computer....

Started by Heirteir, 16 November 2014 - 08:22 PM
Heirteir #1
Posted 16 November 2014 - 09:22 PM
For some reason computer craft keeps giving me this error
testing:58: attempt to call string every time I try to run my Rednet chat program this is my code please tell me why I am getting this error and how I can fix it

done = false

local function mainLoop()
  while true do
    if done then
	  break
    end
    sleep(.5)
  end
end

local function clear(type)
  if type == 0 then
    term.clear()
    term.setCursorPos(1, 1)
  end
  if type == 1 then
    term.clear()
    term.setCursorPos(1, 1)
    print("Rednet Chat -- Created By Heirteir")
    print("For A List Of Commands Type /help")
  end
  if type == 2 then
    x,y = term.getCursorPos()
    term.clearLine()
    term.setCursorPos(x, y-1)
  end
end

local function inputUsername()
  clear(1)
  term.write("Please enter your username: ")
  return read()
end

local function openRednet()
  for _,side in ipairs(rs.getSides()) do
    if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
	  rednet.open(side)
	  return
    end
  end
    clear(0)
    print("No wireless modem found.")
    print("Please install a wireless modem on any side of the computer")
    sleep(2)
    clear(0)
    print("Closing...")
    sleep(1)
    clear(0)
    done = true
end

local function starting()
  openRednet()
end

name = inputUsername()
clear(1)

local function listen()
  while true do
    id, message = rednet.receive()
    print(message)
    sleep(.5)
  end
end

function send()
  while true do
    local input = read()
    if (string.sub(input, 1, 1)) == "/" then
	  clear(2)
	  command(input)
	  if done then
	    clear(0)
	    return
	  end
	  else
	    message = name .. " (" .. os.getComputerID() .. ") >> " .. input
	    clear(2)
	    print("You >> " .. input)
	    rednet.broadcast(message)
    end
    sleep(.5)
  end
end

starting()

parallel.waitForAny(send, listen, mainLoop)

Thank you so much
Dragon53535 #2
Posted 16 November 2014 - 10:12 PM
line 58 is

name = inputUsername()
and inside inputUsername you're doing

return read()
Now while i have no clue if this is your exact problem, but try doing

local inputted = read()
return inputted
inside inputUsername instead of return read()
Bomb Bloke #3
Posted 16 November 2014 - 10:42 PM
Nah, no need to do that - it's more efficient as-is.

This code won't produce the stated error. Reboot the computer and double check that the code you're showing here is the code that you're actually attempting to run.