Replicated ConVars (FCVAR_REPLICATED)

I noticed something odd with the replicated cvars, not sure if it’s me or it functions like this.
This get’s called in a shared file (client/server):



CreateConVar( "m_website","www.google.com", { FCVAR_REPLICATED , FCVAR_SERVER_CAN_EXECUTE } );


Let’s say I change m_website to “www.yahoo.com” server side, and a player joins. He still see’s “www.google.com” until I change the cvar again. (while he’s connected).

So pretty much, players only get the latest value of the cvar if it’s updated WHILE they are connected.
Is this normal? Since it’s causing me lots of problems.

Also, if this is normal. My current workaround is to loop through all the cvars, and “update” the value every 30 seconds or so.
It seems like this would be problematic when there are 32+ players.

First of all you don’t need to sync the cvar every 30 seconds, as you have said, it will update as long as the player is on server, so you only to sync it in PlayerInitialSpawn.

And try calling the function only on server and see if that changes anything.

Well I have 20+ Cvars that are replicated.
I would assume it’s not possible to sync it just for one client. If I did it on PlayerInitialSpawn() I would just call “RunConsoleCommand( “m_website”, GetConVar(“m_website”):GetString() )”

Which then the client gets properly updated. But it would be all clients.

Use SendLua to run the command on the players client?

I’m confused. What command are you wanting me to run? You can’t run “m_website” on the client, because it states it just a replicated cvar that you have no control over.

Have you tried running create cvar only on server?

Yes, but the Valve Wiki (and GMod wiki) state that you have to have the cvar created on both the client and server for it to function properly.

And where exactly did you see that?

http://gmodwiki.net/Lua/Global/CreateConVar

I am running it only serverside in stranded gamemode and it works fine.

In a listen server it functions correctly. To get a server-side cvar on the client, you must create it on both the client/server.
To be honest, I think the issue is that the server sends the data off to the client, even before the cvar is registered. Because the default replicated cvars in source work correctly. (IE: When you change them on the server, when a client connects, they have the latest value).



Server starts. CVar "m_website" is created with default value of "www.google.com"
Type: m_website "www.yahoo.com" into the console (cvar update)

Player joins. 
Player get's the lua files, cvar is created on the client.

Run: GetConVar("m_website"):GetString() - returns "www.google.com" (on client)
Type: m_website "www.yahoo.com" into the server console, AGAIN
Run: GetConVar("m_website"):GetString() - returns "www.yahoo.com" (on client)



Oh, that I did not know.

The easiest way to fix this is to use the cvar3 module with Player:ReplicateData

I do this on my servers…

[lua]local oldCreateCvar = CreateConVar

local replicatedVars = {}

function cvars.GetLuaReplicated()
return replicatedVars
end

function CreateConVar( name, value, flags, help )

local cvar = oldCreateCvar( name, value, flags, help )

if type( flags ) == "table" then
	if table.HasValue( flags, FCVAR_REPLICATED ) then
		table.insert( replicatedVars, cvar )
	end
elseif type( flags ) == "number" then
	if bit.band( flags, FCVAR_REPLICATED ) > 0 then
		table.insert( replicatedVars, cvar )
	end
end

return cvar

end

hook.Add( “PlayerAuthed”, “Breakpoint - Update Replicated”, function( ply, steamid, unique )
for _,cvar in pairs( cvars.GetLuaReplicated() ) do
ply:ReplicateData( cvar:GetName(), cvar:GetString() )
end
end )[/lua]