Attempt to access nil value error

Hello everyone.
Very happy to tell that due to the invaluable help of the Defold community, my game has progressed far ahead in space. It is now almost 40% complete and if I work hard(that’s not so easy :grin:) it might be in beta soon.
So while progressing ahead, I found some thing:
I have a player controller, much similar to GBRAusers as I mentioned before. I integrated it with a Lua Module, which is something like this:

ocal M = {}
local player_ships = 
{
	{
		category = "normal",
		level1 = {
			speed = 100,
			turn_speed = math.rad(200),
			fire_rate = 0.45,
			min_speed = 50,
			health = 10,
			guns = 1
		},
		level2 = {
			speed = 115,
			turn_speed = math.rad(215),
			fire_rate = 0.40,
			min_speed = 50,
			health = 11,
			guns = 1
		},
		level3 = {
			speed = 130,
			turn_speed = math.rad(225),
			fire_rate = 0.35,
			min_speed = 50,
			health = 12,
			guns = 2
		}
	},
}--etc, etc.

This is perfectly fine , as the function in this module returns the table correctly.
But when I link it to my player script:

self.max_speed = self.ship.level1.speed

This works but this doesn’t:

self.ship = player_data.return_planedata(SHIP_NO)
if LEVEL == 3 then
self.max_speed = self.ship.level3.speed
		self.speed = self.ship.level3.speed
		self.health = self.ship.level3.health
		self.MAX_ROTATION_SPEED = self.ship.leve13.turn_speed
		self.RATE_OF_FIRE = self.ship.level3.fire_rate
		self.MIN_SPEED = self.ships.level3.min_speed
	end
end

It stops working and throws an error:

/Scripts/Player/Player.script:101: attempt to index field 'leve13' (a nil value)

Why is it so? Maybe am I missing some thing again?

This is a perfect time to use the debugger and set a breakpoint to inspect the values. Atre they really what you expect?

Definitely no. They are not even close.
It just continues to print out nil when I print the value of self.rotation_speed

Can it be because I’m declaring the variables repeatedly in an if else statement.

This probably means you haven’t set the variable to anything, or that you’ve assigned nil to it.
Backtrack your code, to follow that variable, when it’s initialized, and when it’s updated.

Hey, it just started working automatically. I just changed

turn_speed = math.rad(200),

to

turnspeed = math.rad(200),

And it stopped showing the error. When I change it again to

turn_speed = math.rad(200),

The error pops again. Why is it so when I put just an underscore?
Anyways it is working now, and the player system up and running, with a special thanks to @britzl and @Mathias_Westerdahl
BTW: Is there some thing on how to use the debugger?

Well, you need to spell your variables the same throughout your code. Otherwise you’ll easily end up with new variables containing nil values.

The spelling was exactly the same. That is why when I wrote turn_speed at both the places again, the error resurfaced.(the leve13 appearing above was changed to level3 but to no effect.). Might be something, but at the moment, I am more than happy with my working players.