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

Error: concatenate nil and string.

Started by Zamithal, 29 December 2012 - 12:47 PM
Zamithal #1
Posted 29 December 2012 - 01:47 PM
I have had some help with this code before but it needs a little more love :D/>

rednet.open("back")
invalid = "ERROR: Not a valid floor."
f = {}
f[1] = "Automation"
f[2] = "Storage"
f[3] = "Agriculture"
f[4] = "Reactor control"
f[5] = "Matter generation"
f[6] = "Matter generation 2"
f[7] = "Living space"
f[8] = "Library"
f[9] = "Danger room"
f[10] = "Roof"
f[11] = invalid
f[12] = invalid
f[13] = invalid
f[14] = invalid
f[15] = invalid
f[16] = invalid
g = {}
g[1] = "white"
g[2] = "Lightblue"
g[3] = "yellow"
g[4] = "magenta"
g[5] = "purple"
g[6] = "lime"
g[7] = "orange"
g[8] = "red"
g[9] = "pink"
g[10] = "green"
g[11] = "brown"
g[12] = "cyan"
g[13] = "blue"
g[14] = "black"
g[15] = "gray"
g[16] = "lightGray"
while true do
		e, p1, p2 = os.pullEvent()
		if e == "rednet_message" then
	rednet.send(p1, "Floor " .. p2 .. ", " .. f[p2] .. " accepted.")
				if colors[g[p2]] then rs.setBundledOutput("right", colors[g[p2]]) end
  
		elseif e == "redstone" then
				rs.setBundledOutput("right", rs.getBundledInput("left") == 0 and rs.getBundledOutput("right") or rs.getBundledInput("left"))
		end
end

the error is line 44 " rednet.send(p1, "Floor " .. p2 .. ", " .. f[p2] .. " accepted.")" concatenate nil and string
Kingdaro #2
Posted 29 December 2012 - 02:37 PM
Yep, the problem is with p2. Since it's currently a string, it searches the table for (if p2 were 5) f["5"] instead of f[5]. Simple solution, convert p2 to a number before usage:

e, p1, p2 = os.pullEvent()
p2 = tonumber(p2)

Also, for the love of god, please do this:

f = {
	"Automation",
	"Storage",
	"Agriculture",
	"Reactor control",
	"Matter generation",
	"Matter generation 2",
	"Living space",
	"Library",
	"Danger room",
	"Roof",
	invalid,
	invalid,
	invalid,
	invalid,
	invalid,
	invalid
}
g = {
	"white",
	"Lightblue",
	"yellow",
	"magenta",
	"purple",
	"lime",
	"orange",
	"red",
	"pink",
	"green",
	"brown",
	"cyan",
	"blue",
	"black",
	"gray",
	"lightGray"
}
Not a requirement, just a suggestion.
Zamithal #3
Posted 29 December 2012 - 03:37 PM
Ok, thank you I got it fixed now, but, just out of curiosity, why is that way better for setting values in a table? It seems less organized IMO.
Lyqyd #4
Posted 29 December 2012 - 04:00 PM
Ok, thank you I got it fixed now, but, just out of curiosity, why is that way better for setting values in a table? It seems less organized IMO.

It accomplishes literally the exact same thing. Think about what you'd have to do to add an entry in the middle, though. Your way: add the new entry, re-number all later entries. The better way: add the new entry, done.