Page 1 of 1

"Attempt to index a nil value"-error in head of if loop

Posted: Sun May 10, 2015 8:07 pm
by Pixelwar
Hello,

I get a seemingly well-known error message("attempt to index a nil value") and I found some 'solutions', but no one helps, it's a little bit strange, because I doesn't index something with the critical code. I attach the .love file, hope you can help me.

Thanks :awesome:

Re: "Attempt to index a nil value"-error in head of if loop

Posted: Sun May 10, 2015 8:21 pm
by BOT-Brad

Code: Select all

if i == 0 and j == 0 then
	--Do nothing
elseif cave[neighborX][neighborY] == nil then
	count = count + 1
elseif cave[neighborX][neighborY] == 1 then
	count = count + 1
end
in those elseifs, cave[neighborX] is nil occasionally, so trying to access the [neighborY] of a nil value is throwing the errors. Easily fixed by first checking if cave[neighborX] is notnil.

Code: Select all

if i == 0 and j == 0 then
	--Do nothing
elseif cave[neighborX] and cave[neighborX][neighborY] == nil then
	count = count + 1
elseif cave[neighborX] and cave[neighborX][neighborY] == 1 then
	count = count + 1
end

Re: "Attempt to index a nil value"-error in head of if loop

Posted: Sun May 10, 2015 8:40 pm
by Pixelwar
Works. Thanks for the fast answer.