Sending server variables to the client?

I was wondering, in my shared.lua I can access client and server variables. If i declare a variable in shared.lua, can it be used in both the client and server?

And if not, how do I go about sharing variables with the client and server?

Yes you can.

No, use user messages or networked variables. That is if you want the client to sync with the server values of variables, if not, you don’t need them.

On a scale of easiest to hardest:

Networked Variables -> Console commands -> User Messages -> Datastream

For sending information from the server it’s actually more like this:

Networked Variables -> User Messages -> Datastream-------------------------------------------------------------------------------------------------------------------------> Console commands

Well I’m using User Messages and they work all dandy BUT, I’m having a bit of a problem. Im sending a message from the server to the client in two spots. When the player first spawns and when they respawn. I have a vgui that displays one of the variables that are sent but I get an error of a NIL value after the initial spawn. If I respawn, then the variable gets declared… and I don’t know why?

You are probably sending the usermessage too early. You could delay it with a timer by about two seconds after the initial spawn.

Thanks, worked like a charm :P. The player probably still joining while I was messaging them…

Personally, I like console commands just because they seem to be more reliable than Umsgs when lag occurs. I absolutely hate the “Unclosed Umsg” error or whatever it is.

Datastream just wraps concommand / usermessage. It’s not a separate layer or anything like that.

Overv has the ways to go about this:

Usermessage the values to the client when they change
Use a global variable ( Eg, SetGlobalInt( “Name”, value ) )
Use networked variables ( Eg, SomeEntity:SetNWInt( “Name”, value ) )

It only really comes down to how you want to access this information clientside.

You could always send the information on PlayerSpawn, as it is called every time a player is spawned, even the first time.

Well, I have built an simple engine before which had all the server-client talking and doing player saving and skills and an awesome VGUI, but I gotta rebuild it all from scratch. The time before I used console commands but this time I thought I could try something new, and that I forgot how to do it :stuck_out_tongue: