Garry's Mod Wiki


  Entity:SetNW2String( string key, string value )


Sets a networked string value on the entity.

The value can then be accessed with Entity:GetNW2String both from client and server.

You should not use the NW2 System on entities that are based on a Lua Entity or else NW2Vars could get mixed up, updated multiple times or not be set.

Issue Tracker: 5455
The value will only be updated clientside if the entity is or enters the clients PVS. use Entity:SetNWString instead
Running this function clientside will only set it for the client it is called on.
The value will only be networked if it isn't the same as the current value and unlike SetNW* the value will only be networked once and not every 10 seconds.


1 string key
The key to associate the value with
2 string value
The value to set, up to 511 characters.


If you need more than 511 characters, you can use something like this.
This uses the NW2 Name limit which is 1023 charcters that we can use for networking.

This works by setting the given value as the NW2 Name but this will quickly fill the NW2 Stringtable(networkvars).
If the Stringtable gets filled, you won't be able so set new NW2Vars.
So you should only use this way of networking, if you want to network something like a table once.
local identifier = string.char(13, 13) local meta = FindMetaTable( "Entity" ) function meta:SetNW2BigString( name, value ) self:SetNW2String( value, identifier .. name ) end NW2String_Cache = NW2String_Cache or {} function meta:GetNW2BigString( name, fallback ) fallback = fallback or "" local ent_cache = NW2String_Cache[self] if !ent_cache then return fallback end local var = ent_cache[name] if !var then return fallback end return var.value or fallback end -- new will be our Name and name will be our value to store. hook.Add( "EntityNetworkedVarChanged","BiggerNW2String_Example", function( ent, name, old, new ) if !string.StartWith( new, identifier ) then return end -- we check, if the string starts with the identifier local ent_cache = NW2String_Cache[ent] -- gets the cache of the given Entity. if !ent_cache then ent_cache = {} end new = string.sub( new, 3 ) -- removes the identifier from the string local var = ent_cache[new] -- gets the Var Table if !var then var = {} end var["type"] = "BigString" -- were using the same structure there as the NW2 System. type = [type] and value = [value] var["value"] = name ent_cache[new] = var NW2String_Cache[ent] = ent_cache -- sets the cache of the end ) -- tesing it. if SERVER then local str = "" for k=1, 1023 do -- creates a string with the maximum length str = str .. "a" end Entity(1):SetNW2BigString( "Test", str ) end timer.Simple( 1, function() -- we use a delay because networking takes a bit of time for the client. print( string.len( Entity(1):GetNW2BigString( "Test", "" ) ) ) -- prints the length of the NW2 String end )
Output: Server and Client Output: