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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Pixelwar
Prole
Posts: 7
Joined: Sun May 10, 2015 10:48 am
Contact:

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

Post 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:
Attachments
Cave Generator.love
(1.33 KiB) Downloaded 145 times
@pixelwar_studio on twitter
User avatar
BOT-Brad
Citizen
Posts: 87
Joined: Tue Dec 02, 2014 2:17 pm
Location: England

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

Post 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
Follow me on GitHub! | Send me a friend request on PSN!
User avatar
Pixelwar
Prole
Posts: 7
Joined: Sun May 10, 2015 10:48 am
Contact:

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

Post by Pixelwar »

Works. Thanks for the fast answer.
@pixelwar_studio on twitter
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 6 guests