I'm getting an invalid key to next error

Hi!

Can someone help me spot the error in this code? I know an “invalid key to next” is something related to changing a table which you are currently running through in pairs(). But I can’t see where i am doing that

if message_id == hash("yourinfo")then 
	self.levelno = message[2]
	self.mystatus = message[1]
	self.levelplan = message[3]
	if message[1] == 0 then--level is not available. 
	elseif message[1] > 0 then --level is available, make image! 
		for key, value in pairs(self.levelplan) do
			self.placed = self.placed + 1 
			self.tiletype= {0,0,0,0}
			self.yvalue = (((self.placed%10)*64)+128)+64
			self.xvalue = (math.floor(self.placed/10)*64+128)
			if value == 9 or value == 5 or value == 8 or value == 7 or value == 6 then
				if self.levelplan[key-10] == nil or self.levelplan[key-10] == 0 then
					self.tiletype[3] = 1 --(left)
				end
				if self.levelplan[key+10] == nil or self.levelplan[key+10] == 0 then
					self.tiletype[4] = 1 (right)
				end
				if self.levelplan[key-1] == nil or self.levelplan[key-1] == 0 then
					self.tiletype[2] = 1 --(down)
				end
				if self.levelplan[key+1] == nil or self.levelplan[key+1] == 0 then
					self.tiletype[1] = 1 --(up)
				end
				self.props = { self.tiletype[1],self.tiletype[2],self.tiletype[3],self.tiletype[4] }
				factory.create("#tile", vmath.vector3(self.xvalue, self.yvalue, 0), nil, self.props, vmath.vector3(self.tilescale ,self.tilescale ,1))
			end
		end
	end

end

Hello hello.

The one obvious error I can see is that you have missed to comment out (right) in the
if self.levelplan[key+10]
statement.

– Majk

1 Like

What we’d really like is to have a lint’er in our editor, but there are online editors and compilers for many many languages, Lua being one of them.

Here’s one example of how to find your syntax error (that @mfridh01 already found :wink: ):
https://repl.it (Press Run after pasting the code)

Hi everyone!

I was actually making a mistake with the properties table I was using as part of a factory.create. I tried to edit the post, but it looks like I didn’t save the edit.

Mathias, thanks for your link! It looks like a great way for me to get more info about errors.

1 Like