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

Error startup:54:Attempt to concatenate a string and nil

Started by micmou, 15 May 2014 - 03:37 AM
micmou #1
Posted 15 May 2014 - 05:37 AM
I got my Screen program working but after a while it gives the above error it is at a random time at which it does it. The error occurs after you name it and nothing is being done.


Screen

monitor= peripheral.wrap("top")
monitor.clear()
monitor.setCursorPos(2, 2)
monitor.setTextScale(2)
rednet.open ("left")
while true do
  event, id, text = os.pullEvent()
  shopID = id
  if shopID == 56 then
	id = 101
  elseif shopID == 57 then
	id = 102
  elseif shopID == 58 then
	id = 103
  elseif shopID == 59 then
	id = 104
  elseif shopID == 60 then
	id = 105
  elseif shopID == 61 then
	id = 106
  elseif shopID == 19 then
	id = 107  
  elseif shopID == 18 then
	id = 108
  elseif shopID == 17 then
	id = 109
  elseif shopID == 16 then
	id = 110
  elseif shopID == 15 then
	id = 111
  elseif shopID == 14 then
	id = 112
  end
  if event == "rednet_message" then
	if id == 101 then
	  monitor.setCursorPos(2, 2)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
	elseif id == 102 then
	  monitor.setCursorPos(2, 3)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
	elseif id == 103 then
	  monitor.setCursorPos(2, 4)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
	elseif id == 104 then
	  monitor.setCursorPos(2, 5)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
	elseif id == 105 then
	  monitor.setCursorPos(2, 6)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
	elseif id == 106 then
	  monitor.setCursorPos(2, 7)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
	elseif id == 107 then
	  monitor.setCursorPos(2, 8)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
	elseif id == 108 then
	  monitor.setCursorPos(2, 9)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
	elseif id == 109 then
	  monitor.setCursorPos(2, 10)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
	elseif id == 110 then
	  monitor.setCursorPos(2, 11)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
	elseif id == 111 then
	  monitor.setCursorPos(2, 12)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
	elseif id == 112 then
	  monitor.setCursorPos(2, 13)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
	end
  end
end

Computer

local PName = ""
local Input = ""
local Pass = "da5655"

function check()
  if not fs.exists("Name") then
	local file = io.open("Name", "w")
	file:write(PName)
	file:close()
  end
end

function savef()
  local file = io.open("Name", "w")
  file:write(PName)
  file:close()
end

function readf()
  local file = io.open("Name", "r")
  PName = file:read()
  file:close()
end



function send()
  rednet.open("bottom")
  rednet.send(73,PName)
  rednet.close("bottom")
end

  
check()
readf()
send()
while true do
  term.clear()
  term.setCursorPos(1,1)
  term.write("Enter the Password to change the name.")
  Input = read("*")
  if Input == Pass then
	term.clear()
	term.setCursorPos(1,1)
	term.write("Enter the name of your store.")
	PName = read()
	savef()
	send()
  else
	term.write("Wrong Password")
	sleep(3)
	os.reboot()
  end
end

This is what another member of the server said he is helping me
Update: we did a print(id, text) on monitor comp and at a point it printed only the id not the text and we got the error again. the nil was the text but dont know why because it worked fine for like 4 tries

As I said it doesnt happen when the info is being sent the when it gives the original error
Edited on 15 May 2014 - 03:48 AM
RAWK-HIGH #2
Posted 15 May 2014 - 06:03 AM
Hello i'm one of the programmers. In this image you can see that it on the monitor term it prints the id and text twice till the last and only print the id and leaves blanks for the rest than errors. Its as if the information is disappearing. Its random and is never the same amount of times. micmou has gone to bed so i hope ill be able to answer any questions or statements. Thanks RAWKHIGH

[Choose better sample text for your screenshot, if you don't mind. :P/> -L]
Edited on 15 May 2014 - 05:16 AM
gezepi #3
Posted 15 May 2014 - 06:46 AM
If you want to avoid crashing your program when text is not received change this line

  if event == "rednet_message" then
to

  if event == "rednet_message" and text then

What it's doing is testing if the event is of the correct type (as it was before) and it will only proceed if text is … anything. In Lua, something is not true if it is false or nil (are there more? I forget). Which means that anything that isn't false or nil is therefore true. With this in your code if the text variable contains a nil value it will not get to the part where it tries to concatenate it with anything.

This won't fix your problem but It might help find where the issue is coming from. I would suggest printing event, id and text each time an event is triggered. You may be getting stray messages that don't have a value for text to take on.

A few things to check in your Computer program: Is the correct new name being saved to disk? If you print PNAME in your send function is it correct?
Edited on 15 May 2014 - 04:50 AM