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.