Network Variables -- am I using this correctly?

As I understand it, network variables are limited to 4 at a time for each type (int, string, boolean, etc…) and I’m trying to add a custom variable for a playerclass in my gamemode (I think).

For example, if I want my players to have a “cash” variable, would I use network variables to keep track of that serverside and clientside, or would I use something else? It seems the limit on network variables / datatables would cause problems later on since I might also add more custom variables than the limit would allow me to. Also, why exactly is the limit there in the first place?

Here’s what I have:


function PLAYER:SetupDataTables()

 BaseClass.SetupDataTables( self )
 self.Player:NetworkVar( "Int", 0, "Cash" )

end

Is there a better way to handle custom player variables that I’m not finding?

For something like cash would be a Networked Variable be a better option?
Even on the wiki it uses money as an example, Take a look at this

Well theres NetworkedVariables although garry might kill you if you use it.

I guess a better way is to send it via the net library and have it saved on the client

Serverside:

[lua]
util.AddNetworkString(“PlayerInfo”)

local PlayerMetaTable = FindMetaTable(“Player”)
function PlayerMetaTable:SavePlayer()
net.Start(“PlayerInfo”)
net.WriteInt(self:GetTable().Money, 32)
net.Send(self)
end
[/lua]

Clientside:
[lua]
net.Receive(“PlayerInfo”, function(len)
LocalPlayer():GetTable().Money = net.ReadInt(32)
end)
[/lua]

You don’t need to put self:GetTable().someVar, you can just put self.someVar

NetworkVar’s are buggy when used in playerclasses. Garry’s interface isn’t complete and SetupDataTables is only called on the player itself. NutScript has a great workaround for this:

Basically it checks if the Get* exists on the client, if it doesn’t, it calls ns_sendplydt which calls the server which calls all players to update their datatables.

As you see, its abit unneccesarily complicated. Unless you really need the advantages of NetworkVars, just network the variables yourself, it’s easier.