Msqloo question and player variables

Hello, I am having trouble creating a player variable after pulling data from a mysql query. (Most of this code if from the UTime version with MySQL)


function PlayerJoined( ply )
	if(ply:IsBot()) then return end
	local uid = ply:UniqueID()
	local sid = ply:SteamID()
	local reputation
	local rep_points
	query( "SELECT rep, rep_points FROM players WHERE uid = " .. uid, function( data )
		
		if table.Count( data ) > 0 then -- player exists
			row = data[ 1 ]
			reputation = row.rep
			rep_points = row.rep_points
			print( "[Reputation] " .. ply:Nick() .. " connected with " .. row.rep .. " reputation and " .. row.rep_points .. " points.")
			
		else -- player does not exist
			-- create the player
			query( "INSERT into players ( uid, rep, rep_points, steamid, playername ) VALUES ( " ..
				uid .. ", 0, " .. "0" .. ", '" .. sid .. "', '" .. db:escape( ply:Nick() ) .. "' )", 
				function() print( "[Reputation] Successfully created new player " .. ply:Nick() .. "." ) end )
		end
		
		ply.RepLoaded = true
		ply.Reputation = reputation
		ply.RepPoints = rep_points
	end )
end
hook.Add( "PlayerInitialSpawn", "RepInitialSpawn", PlayerJoined )

If I try to load the RepLoaded variable anywhere outside of that hook it returns nil.
If I create a function in the sv file I have in autorun and have it call that function to create the variable it still returns nil.

I have spent a few hours trying to figure out how to set a variable to the player that will load properly into the rest of the system (a sv_repHooks.lua that uses one of the SQL variables to add to the players reputation) but nothing will load, even if I create a function inside the SQL lua file shown it returns nil anywhere outside the hook.

What I am wondering is how could I set a variable to the player with the values rep and rep_points from the MySQL without it returning nil on other serverside scripts. I am not trying to load it anywhere clientside at this point. Is the variable not being shared to other serverside scripts for that player because it’s in a hook? Because it’s in autorun? This is my first time trying to make some sort of addon and using any other method besides MySQL isn’t what I want to do. The variables I am loading from the SQL query are loaded properly and work, but if I try to set ply.Reputation = row.rep then the ply.Reputation variable only works inside that PlayerInitalSpawn hook and returns nil in any other serverside script. I am not too well versed on networking on garry’s mod yet and I am hoping to fix that.

Also I apologize if it is rude to take snippets of code from other scripts but after reading over what he did I understood it to the point where anything I made would be very similar in the first place so I decided to save some time.

Well after reading it over I seem to have made it overcomplicated.

I have that function in sv_repSQL.lua in autorun > server in an addon folder, any variables I declare in that lua file at all returns nil on other serverside scripts. I tried setting it to ply.Reputation = row.rep, creating a separate function that gets called and defines the variables, nothing works.

What could cause a serverside player variable like that to return nil on my other serverside files?

Update: I moved my doplayerdeath hook that I was trying to load the variable with inside of the lua file that I set the variable in and it still returns nil.


local tag = "[Reputation] "

function ReputationPoints(ply, attacker, dmginfo)
	if(attacker == ply) then return end
	local teamkill = tag .. "You lost 2 reputation points from teamkilling"
	local kill = tag .. "You gained 1 reputation point for killing a terrorist"
	local innokill = tag .. "You lost 1 reputation point for killing a fellow innocent"
	local repchange = 0
	if IsValid(attacker) and attacker:IsPlayer() then
		if attacker:IsActiveTraitor() then
			if ply:GetTraitor() then
				attacker:ChatPrint(teamkill)
				repchange = -2
			elseif ply:GetDetective() then
				attacker:ChatPrint(kill)
				repchange = 1
			elseif ply:IsTerror() then
				attacker:ChatPrint(kill)
				repchange = 1
			end
		end
		
		if attacker:IsActiveDetective() then
			if ply:GetDetective() then
				attacker:ChatPrint(teamkill)
				repchange = -2
			elseif ply:GetTraitor() then
				attacker:ChatPrint(kill)
				repchange = 1
			elseif ply:IsTerror() then
				attacker:ChatPrint(innokill)
				repchange = -1
			end
		end

		if(!attacker:IsActiveDetective() and !attacker:IsActiveTraitor() and attacker:IsTerror()) then
			if(ply:GetDetective()) then
				attacker:ChatPrint(teamkill)
				repchange = -2
			elseif(ply:GetTraitor()) then
				attacker:ChatPrint(kill)
				repchange = 1
			elseif(ply:IsTerror()) then
				attacker:ChatPrint(innokill)
				repchange = -1
			end
		end
	end
	print(ply.Reputation)
end
hook.Add("DoPlayerDeath","ReputationPoints", ReputationPoints)

But it will load the variables in this function


function UpdateAll()
	for _, ply in pairs( player.GetAll() ) do
		if IsValid( ply ) and ply:IsConnected() and ply.RepLoaded then
			print(ply.RepLoaded)
			print(ply.Reputation)
			print(ply.RepPoints)
			-- UpdatePlayer( ply )
		end
	end
end
timer.Create( "RepTimer", 300, 0, UpdateAll )

Both are in the same .lua file as the script in the first post.

Update #2: I put a k, v for pairs(player.GetAll()) into the doplayerdeath hook and that loads the variables on each death fine…does that hook not properly grab the player entities or something? Do I need to loop through all the players every time I need to load that variable? Unsure as to why it won’t load properly.

[editline]29th October 2014[/editline]

Nvm I am an idiot, ply wasn’t the entity of the attacker it was the entity of the victim and I didn’t notice that, was killing bots this whole time so it would return nil since I have a bot exclusion in the mysql script.