nil value

I just started lua im very stupid so if you can help me i would be happy.

This is the error :


[ERROR] addons/test/lua/autorun/server/test.lua:27: attempt to perform arithmetic on global 'rank' (a nil value)


hook.Add( "PlayerDeath", "GiveRank", function ( ply, weapon, killer )
	if killer:IsPlayer() then
		if killer:GetNWInt("hax") >= 250 then
		    killer:SetNWInt( "rank", killer:GetNWInt( "rank" ) + 1 )
			killer:PrintMessage( HUD_PRINTTALK, "Congrats. You just got rank up" )
			end
		end
	end )

Have you done rank loading yet? By the looks of things, the NWValue “rank” is not set by the time the hook is called.

how would i do that? im newb

Hang on, I posted some code with exactly what you are looking for not so long ago.


Here we go:



experience = {}
experience.Settings = {}
experience.Hooks = {}

function experience.Think()
	for k,v in pairs(player.GetAll()) do
		if !v.Loaded then continue end
		if experience.GetXP(v) >= ((experience.Settings["xpdiff"] * experience.Settings["xpgeo"])^experience.GetLevel(v)) then
			experience.SetLevel(v,experience.GetLevel(v)+1)
			experience.SetXP(v,0)
			v:EmitSound(experience.Settings["sound"],experience.Settings["range"],experience.Settings["volume"])
		end
	end
end

function experience.Settings()

	experience.Settings["directory"] 	= 	"xp"
	experience.Settings["maxlevel"] 	=	10
	experience.Settings["xpgeo"]		= 	1.2 --Geometric progression of max xp.
	experience.Settings["xpdiff"] 		=	50
	experience.Settings["sound"]		= 	"ambient/atmosphere/thunder1.wav"
	experience.Settings["range"]		= 	500
	experience.Settings["volume"]		=	200
	
	experience.Hooks["Think"] = 			{true,	function(...)
												experience.Think()
											end}
	experience.Hooks["PlayerInitialSpawn"] = {true,	function(...)
												experience.InitP({...}[1])
											end}
	experience.Hooks["PlayerDisconnect"] = {true,	function(...)
												experience.SavePlayer({...}[1])
											end}
end

function experience.InitS()

	experience.Settings()
	if !file.Exists( experience.Settings["directory"] , "DATA" ) then
		file.CreateDir( experience.Settings["directory"] ) 
	end
	
	for k,v in pairs(experience.Hooks) do
		if v[1] == true then
			hook.Add(k, "EXP"..k, function(...)
				v[2](...)
			end)
		end
	end
end

function experience.InitP(ply)

	local id = ply:SteamID64()
	if !file.Exists( experience.Settings["directory"] .. "/" .. id .. ".txt" , "DATA" ) then
		file.Write( experience.Settings["directory"] .. "/" .. id .. ".txt" , "0
1" )
		ply:SetNWInt("XP",0)
		ply:SetNWInt("Level",1)
	else
		local data = file.Read( experience.Settings["directory"] .. "/" .. id .. ".txt")
		data = string.Explode( "
", data )
		experience.SetXP(ply,data[1])
		experience.SetLevel(ply,data[2])
	end

	ply.Loaded = true
end

function experience.SavePlayer(ply)

	if !ply.Loaded then return end
	local id = ply:SteamID64()
	local data = experience.GetXP(ply).."
"..experience.GetLevel(ply)
	file.Write( experience.Settings["directory"] .. "/" .. id .. ".txt",data)
	
end

function experience.SetLevel(ply,n)
	ply:SetNWInt("Level",n)
end

function experience.SetXP(ply,n)
	ply:SetNWInt("XP",n)
end



You may need to edit things here and there, and fix the hook loading, but should work fine.
I should also note: This is untested, PM with any errors if it doesn’t work :slight_smile:

Can´t you just help with my problem. I want to learn lua not getting fed by other posting code. Why aren´t my code not working? And what can be done to fix it?

Ok, Sorry.

When a player dies, you are calling:



 killer:SetNWInt( "rank", killer:GetNWInt( "rank" ) + 1 )


The part that is erroring is this:



killer:GetNWInt( "rank" )


Because you have not set that value. Look through my code to see how player loading and saving is done, should give you an idea on what to do.

Add this:

[lua]
local value = whatever you want to place here
hook.Add(“PlayerInitSpawn”, “SetNWIntSpawn”, function( ply )
if !ply:GetNWInt(“rank”) then
ply:SetNWInt(“rank”, value)
end
end)
[/lua]

Just did look at some other codes about a xp system i found :

My full code here edited to XP and LVL.

Serverside :


XPPerkill = 10
VIPXPerkill = 15
_XGroups = { "user", "member" }
_XVIPGroups = { "VIP", "Owner" }


hook.Add( "PlayerInitialSpawn", "LoadXP", function( ply )
	if ( ply:GetPData( "XP" ) == nil ) then
		if ( ply:GetPData( "LVL" ) == nil ) then
		ply:SetPData( "XP", 50 )
		ply:SetNWInt( "XP", 50 )
		ply:PrintMessage( HUD_PRINTTALK, "Welcome." )
	else
		ply:SetNWInt( "XP" , ply:GetPData("XP") )
		ply:SetNWInt( "LVL" , ply:GetPData("LVL") )
		end
	end
	end )

hook.Add( "PlayerDisconnected", "SaveXP", function( ply )
	ply:SaveXP()
	print( "Player disconnected. Saving XP, perks and LVL to SQLIte database")
end )

hook.Add( "PlayerDeath", "GiveLevel", function ( ply, weapon, killer )
	if killer:IsPlayer() then
		if killer:GetNWInt("XP") >= LEVELS[LVL + 1] then
		    killer:SetNWInt( "LVL", killer:GetNWInt( "LVL" ) + 1 )
			killer:PrintMessage( HUD_PRINTTALK, "Level up" )
			end
		end
	end )

hook.Add( "PlayerDeath", "GiveXP", function ( ply, weapon, killer )
	if killer:IsPlayer() then
    if ( table.HasValue ( _XGroups, killer:GetUserGroup( ) ) ) then
        killer:SetNWInt("XP", killer:GetNWInt("XP" + XPPerkill ) )
        elseif ( table.HasValue ( _XVIPGroups, killer:GetUserGroup( ) ) || killer:IsAdmin( ) || killer:IsSuperAdmin( ) ) then
        killer:SetNWInt("XP", killer:GetNWInt("XP" + VIPXPPerkill ) )
			end
		end	
	end )

Shared :


LEVELS = {}
LEVELS[1] = 250
LEVELS[2] = 450



local Player = FindMetaTable( "Player" )


function Player:SaveScore()
	self:SetPData( "XP", self:GetNWInt( "XP" ) )
	self:SetPData( "LVL", self:GetNWInt( "LVL" ) )
end

function Player:SafeXP()
	self:SetNWInt( "XP", self:GetNWInt("XP" ) )
	self:SetNWInt( "LVL", self:GetNWInt("LVL" ) )
end

And no clientside. This could help you guys to point it out cause i dont understand alex

SetNWInt does not save anything. All it does it network variables.

I know thats why PData does get the NWInt and sets the PData as that so it will save?


(User was permabanned for this post ("Alt of perma'd user Snorlaxs" - Orkel))

Oh shit sorry I didn’t see that part.

I guess I will just stop posting now :confused:

[lua]hook.Add( “PlayerDeath”, “GiveXP”, function ( ply, weapon, killer )
if killer:IsPlayer() then
if ( table.HasValue ( _XGroups, killer:GetUserGroup( ) ) ) then
killer:SetNWInt(“XP”, killer:GetNWInt(“XP”) + XPPerkill )
elseif ( table.HasValue ( _XVIPGroups, killer:GetUserGroup( ) ) || killer:IsAdmin( ) || killer:IsSuperAdmin( ) ) then
killer:SetNWInt(“XP”, killer:GetNWInt(“XP”) + VIPXPPerkill )
end
end
end )[/lua]

Your GetNWInts inside of the SetNWInts were not being closed properly.