attempt to perform arithmetic on global 'playerLevel' (a nil value)?

So I can not remeber what a nil value is, or what it means by “attempt to perform arithmetic on globl ‘playerLevel’”. New to lua, and slowly learning. So any help is awesome!

[lua]
[ERROR] gamemodes/tdm/gamemode/player.lua:6: attempt to perform arithmetic on global ‘playerLevel’ (a nil value)

  1. unknown - gamemodes/tdm/gamemode/player.lua:6
  2. include - [C]:-1
    3. unknown - gamemodes/tdm/gamemode/init.lua:6
    [/lua]

player.lua
[lua]
– include( “init.lua” ) [Without this dashed out, the server crashes on start…]
include( “database/database.lua” )

xpRequired = 4000+(5010.10101*playerLevel) – This is line 6
playerXP = ply:databaseGetValue( “xp” )
playerLevel = ply:databaseGetValue( “level” )
remainingXP = xpRequired - playerXP

[/lua]

init.lua
[lua]
include( “player.lua” )
[/lua]

Nil value = undefined. In this case, you’re using playerLevel before it is defined. Move it below the part where you assign it to a value, and it should work.

Thanks, feel so stupid right now.

[editline]13th November 2014[/editline]

I get a another error after this, and I have messed around with it but I’m not sure on how to fix this. So I get the varible is undefined, but I don’t get how I could make it defined.

[lua]
[ERROR] gamemodes/tdm/gamemode/database/database.lua:158: attempt to index local ‘d’ (a nil value)

  1. databaseGetValue - gamemodes/tdm/gamemode/database/database.lua:158
  2. unknown - gamemodes/tdm/gamemode/player.lua:5
    3. include - [C]:-1
    4. unknown - gamemodes/tdm/gamemode/init.lua:6
    [/lua]

Database:
[lua]
function ply:databaseGetValue( name )

local d = self:databaseGet()
return d[name] – Attempt to index local ‘d’ (a nil value) [Line 158]

end
[/lua]

Thanks again!

self:databaseGet() probably needs argument so it knows what data to get

Well, the good news is you’re not getting an error for self so you did FindMetaTable correctly. But databaseGet isn’t returning a value…

I’d recommend setting up a system / method of grabbing data so that if something is unset, use the default value until it is set.

for example, for d you could do: local d = self:databaseGet( ) || { [ name ] = true; }; where true should be the default data for that particular tag…

Example, take a look at some of these layouts ( will be uploading the net system shortly so all of this will be populated )

data is probably like your databaseGet. It is the container for where all of the data is stored. It only modifies the data ( when it is populated ) when the data is set / changes. But, you could always be able to return a default / fallback to prevent errors…

Take a look at these now.

Also skeleton until I upload the net system

function META_ENTITY:SetFlag( _flag, _value, _private )

SetFlag changes the data; private triggers which database it goes into… if it is private data then only linked entities will know about it. So if the server sets private data on the player, that player knows and no other player knows. If the player gets into a vehicle, they become linked so the player then gets vehicle private data until the player exits. Private is just so certain data doesn’t have to be shared because it really doesn’t need to be.

If it isn’t private, it is public. All changes are synced to all clients if set by server. If any data is set by the client, nothing is networked, the client can make changes to the record for themselves ( because the server is the one that tells the client to do that, and because we don’t ever want to trust user input ).

function META_ENTITY:GetFlag( _flag, _default, _private, _delay ) – ignore _delay

This is the main one to pay attention to. A simple getter. Same format as set except slightly different. It needs a flag, that is the key for what the data is which can be a string or whatever. It should also have a default value just in case no data exists in the selected database ( private or public ) which it’ll return in that event. Ignore _delay, that is for requesting updates to private data temporarily, kind of like how NWVars works but with caching and not recommended to be used.

So, a simple get would look like this:
local _vehicles = _p:GetFlag( “player_vehicles”, { }, true );

_vehicles is guaranteed to be a table regardless of whether or not data exists because there is a default field and it has been coded so that if no data exists, it falls back on the default. But, if data exists, then the default is ignored.

So you can see what goes on under the hood a bit on how you can set up some system which returns a default if no value is set:


//
// Returns the entity flag value ( This is called by META_ENTITY:GetFlag( _flag, _default, _private ) )
//
function data:GetFlag( _ent, _flag, _default, _private )
	// This is simply a glorified EntIndex of the entity
	local _id 		= self:GetEntityID( _ent );

	// This grabs the database; this is guaranteed to exist as a table
	local _flags 	= self:GetFlags( _private );

	// As long as we have a valid entity ( Invalid returns -1 in the GetEntityID function )
	if ( _id >= 0 ) then
		// If our database is setup, and the GetID exists, and either the flag exists / is set, or it is false ( because values can switch between true/false and they still need to be updated )
		if ( _flags && _flags[ _id ] && ( _flags[ _id ][ _flag ] || _flags[ _id ][ _flag ] == false ) ) then
			// Then we have a value data-point to return
			return _flags[ _id ][ _flag ];
		end
	end

	// Otherwise we do NOT have data on record, so we'll return default data to prevent errors.
	return _default;
end

Basically, you need to ensure your tables are initialized ( my_table = my_table || { }; which, if global, ensures that it will work on auto-refresh by setting itself to itself if it exists otherwise it’ll use a blank table ), then just check for data, verify everything!

Alright thanks guys! I will try your suggestions out, and report back soon.