Question: Pulling database object from server-side to hud

I know Mysqloo isn’t supported here, but I have that part worked out and am just having a minor issue.

Server-side I have called what I need to:

DB = mysqloo.connect(mysql_hostname, mysql_username, mysql_password, mysql_database, mysql_port)

I can use db fine to connect, query a table insert, and pull without issue. Where my issue lies is that I am saving stats_deaths and stats_kills to my database and it’s saving just fine. However, I want to pull these objects FROM the database within my cl_hud.lua and display them as their number somewhere on the hud. However I can’t do a select * from table using my assigned db var because well it’s server-side and I’d just get a nil return error.

So my question is, what is the best way, to pull the values from the database, so that I can assign them and use them within my hud. In this case being stats_kills and stats_deaths (the database fields I need pulled)


You have to network the values to the client from the server. Even if you could pass a database handle to the client, it would be the worst idea ever. You could network the variables with the net library, the usmg library (somewhat obselete), or even pl:SetNWInt().

Thanks for the reply James. Another noob question. Should I use SetNWInt in a new sh shared file or should I place it somewhere server-side. At the moment I just have my mysqloo file which includes the connection details, then in sv_player I use an insert query (on duplicate) to auto increment +1 when a kill or death is made at the moment.

Serverside. Putting it in shared wouldn’t break anything, but there’s no need for it to be sent to the client. If you’re using Pl:SetNWInt(string,int), to get the networked integer on the client, you do Pl:GetNWInt( string, default) where string is the string you used serverside, and default is the value to return if the value hasn’t been networked yet.

Ok, and one last thing, My concern… well and a question.
(I assume this is correct) - I run a select query, and assign stats_kills and stats_deaths to two network integers that I can tell client-side… since the hud is constantly updating, this means it would look for that constantly, meaning query the database many many times to keep updating that number (depending on the function I hook to), what function should I hook to, or how do I go about doing this to where it doesn’t pound the database. Example: using OnThink… or OnPlayerSpawn his numbers would update. I’d prefer after each death/kill the number updates right then and there. I am just afraid if it’s always querying the database it could cause stability issues.

You don’t need to query the database everytime the player needs the information, you only need to update the information when it changes.

–client hud (maybe in HUDDraw )
local stats_kills = ThePlayer:GetNWInt( “stats_kills”, 0 )

– server side

function YourFunctionToLoadMySQL()

— Loads into table


local plMeta = FindMetaTable(“Player”)

function plMeta:IncreaseStatsKills( iAmount )
– update the table
local previous
self:SetNWInt( “stats_kills”, previous + iAmount )

Thanks James, you’ve been most helpful. I started working on the code prior to you posting this and ended up doing the same thing just updating the Network Int. Just about finished and working great thus far. Appreciate your help with this. :slight_smile:

No worries. Because this is your first time posting a help thread (I assume), don’t forget to mark it as solved so it closes.