How to send GetPData to client ?

Hi guys,

I’m MisterTakaashi, and I’m trying to create a little gamemode for my server. But I have a problem. I would like a money system and I want to print it on the HUD, this problem is that the HUD is on client and when I try to retrieve this var, is send me nil, It seems I can’t retrieve server variable on my client side. So I can I do that pleaaaaase ?

Thanks you for all replies :slight_smile:

Well, what is your current code, and no matter how many a’s you put in please it isn’t gonna make people want to help you more

Ok, thanks for reply.

At first spawn of the player, I make a ‘If he doesnt’ have an account yet, I create it:


function GM:PlayerLoadout(ply) --Quand le joueur spawn après changement de métier
        if (ply:GetPData( "money", "Pas de data" ) == "Pas de data") then
            ply:SetPData( "money", 2500 )
            print("Vous n'avez pas de compte ou vos informations n'ont pas été chargées. 2500€ ont été déposés dans votre banque")
            print("Vous avez "..ply:GetPData( "money", 0).."€ sur votre compte")

After that, I would like to retrieve the var “money” in my cl_hud.lua:


local PlayerMoney = formatNumber(LocalPlayer():GetPData( "money", 0)) --Argent du joueur

And, yes I’m french :yarr:

Thank you !

Set/GetPData is server side only, you can network the value to the client by using SetNWInt and then read it with GetNWInt

Ok, thank you very much but I have another problem :slight_smile:

I put that in my init.lua:

ply:SetNWInt("money", 30) 

And that in my hud.lua:

local PlayerMoney = formatNumber(LocalPlayer():GetNWInt( "money", 0 )) --Argent du joueur

But HUD print always 0… snif, I don’t understand --’

But love U for reply

Do not use NWInt’s because they can cause reliable snapshot overflows when used too much.

I would suggest using usermessages/net library to send messages to the client and have the client process the information.

Use something like this:

local PLAYER = debug.getregistry().PLAYER
local money = 0

if SERVER then
	util.AddNetworkString( "player_money" )

	function PLAYER:SetMoney( money )
		self:SetPData( "money", money )

		net.Start( "player_money" )
			net.WriteInt( money, 32 )
		net.Send( self )

function PLAYER:GetMoney()
	if SERVER then
		return self:GetPData( "money", money )

	return money or 0

if CLIENT then
	net.Receive( "player_money", function( Length )
		money = net.ReadInt( 32 )
	end )

Just curious, is there any benefits to using [LUA]debug.getregistry().PLAYER[/LUA] as compared to just doing [LUA]FindMetaTable(“Player”)[/LUA]

Probably talking like millisecond speed difference, however I’ve generally gotten used to using the basic functions. _R.Player was replaced with debug.getregistry().Player in Gmod13 update.

Under the assumption that you by “speed difference” mean the access time for the table then you’re wrong (my assumption might be wrong though).

print( debug.getregistry().Player == FindMetaTable( "Player" ) )

> true

How does being equal translate to speed? Both of them return the same thing, so they’re equal. How exactly does that mean there is no speed difference? If all FindMetaTable does is return that field then the speed difference is nothing fancy, but if FindMetaTable does other things then their may be a more significant speed difference than simply returning a field.

I don’t know or care for this speed difference (if it exists) but I’m just pointing out your flawed logic.

do you have any idea how tables work in Lua at all?

Sorry, I’m retarded. Please explain to me how your code relates in any way at all to speed, EvacX the god.

Tables are passed by reference in Lua, the 2 values are the same. FindMetaTable literally cannot do anything to affect the speed without modifying the table (which it doesn’t).

Don’t be a sarcastic smartass, please.

I am absolutely fucking clueless on where you are measuring speed by checking if the field in debug.getregistry()'s returned table is equal to the return value of FindMetaTable.

You could have never posted those, but you decided questioning how smart I am would be better for a discussion, and then have the audacity to ask I stop being sarcastic, so uhhh, fuck you.

It means they’re the same thing and thus have same access time. Seems fairly obvious to me, it’s like racing a car against itself.

I wasn’t rude initially, I commented on a (possibly) misleading post in order to correct him in the case that my assumption was correct.

I’m not questioning how smart you are, but it appears you don’t know exactly how Lua tables work since you keep questioning why 2 tables being the same thing would mean they have the same access times. If anything, you’re the one being rude with your sarcastic remarks in response to my legit (maybe slightly-offensive, but that wasn’t intended) comment about you questioning my “flawed logic”:

It showed that they’re equal; true. That really shows nothing about speed vs each other…

FindMetaTable(x) is slower than debug.getregistry().x (in my own testing).
There is no difference in the output of the two methods (only true in the case of accessing meta-tables). This means that once you have defined the meta-table:

local meta = FindMetaTable("Player")
-- or
local meta = debug.getregistry().Player

There is no speed difference between accessing / writing to either result.

Unless you’re doing something wrong, you’ll be calling these functions a very small amount of times, the speed of each really doesn’t matter, you should be using whatever makes the most sense to you (which I would argue should be FindMetaTable)

We’re talking about access speeds, not the time it takes to retrieve the table and thus what you just said is consistent with what I have said so far.

Love the little debate, but we are actually talking about how to help the OP with his problem (Keep it to the pm’s)

Run this to test it out then:

[lua]local CurTime = _G.CurTime

local time = CurTime()
local meta = debug.getregistry().Player
print( "Getregistry: " … tostring( CurTime() - time ) )

local time = CurTime()
local meta = FindMetaTable( “Player” )
print( "FindMetaTable: " … tostring( CurTime() - time ) )[/lua]