Usermessage problem

Hello,
I’m currently trying to use usermessages to set specific variables on players for my gamemode.
It is working properly, but the thing that puzzles me is that I have to die and respawn once to make the umsgs work properly.
I have added prints to debug the code and came to the conclusion that the received entity on the clientside is NULL until the player dies and respawns at least once.

Clientside code:
[lua]
local function GetPlyVar(udata)
print(tostring(udata)) – for debugging
local ply = udata:ReadEntity() --returns NULL until the player has respawned once
if not ValidEntity(ply) then return end --this is where the function returns until the player respawns
ply.PlyVars = ply.PlyVars or {}
local var, value = udata:ReadString(), udata:ReadString()
ply.PlyVars[var] = value
ply:ChatPrint("Client received umsg: "…ply:GetName()… " var: “…var…” value "… value)
end
usermessage.Hook(“PlyVars”, GetPlyVar)
[/lua]

Serverside code:
[lua]
local meta = FindMetaTable(“Player”)
function meta:SetPlyVar(var, value)
if not ValidEntity(self) then return end
self.PlyVars = self.PlyVars or {}
self.PlyVars[var] = value

if value == nil then value = "nil"
else value = tostring(value) end

umsg.Start("PlyVars")
	umsg.Entity(self)
	umsg.String(var)
	umsg.String(value)
umsg.End()

print("Server sent umsg: "..tostring(self).. " var: "..var.." value: ".. value) -- Prints the correct values to console and should be working properly 
return

end
[/lua]

This really isn’t any better than networked variables. Why not use them?

dont tell him that he will use them everywhere lol XD, plus nwvars are global, maybe he doesnt want that.

im just going to go by the assumption that you do NOT want it global

Server code
[lua]
require(“glon”);

function _R.Player:SetPlyVar(var, value) // _R.Player is the same as FindMetaTable(“Player”);
if not ValidEntity(self) then return end // shouldnt need this but ill leave it in

value = tostring(value);

self.PlyVars = self.PlyVars or {};
self.PlyVars[var] = value;
 
umsg.Start("PlyVars", self)
    umsg.String(glon.encode(self.PlyVars));
umsg.End()
 
print("Server sent umsg: "..tostring(self).. " var: "..var.." value: ".. value) -- Prints the correct values to console and should be working properly 
return

end
[/lua]

Client code
[lua]
require(“glon”);

usermessage.Hook(“PlyVars”, function(udata)
// udata is a bf_read object!
local received = glon.decode(udata:ReadString());
LocalPlayer().PlyVars = received;
print("Client received: "…received);
end
[/lua]

IF, however you want it to be global then user nwvars (but dont use too many). only use them for essential things