Garry's Mod Wiki

Entity:SetNW2String

  Entity:SetNW2String( string key, string value )

Description

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.

Arguments

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

Example

Adds a tag after player's hud target (When you aim at someone)

hook.Add("PlayerSay", "WriteTag", function(ply, text) if not text:StartsWith("!tag ") then return end --Gets the text after "!tag " local tag = text:sub(6) if tag == "" then return "Tag cannot be empty!" elseif #tag > 511 then --Text cannot be longer than 511 characters return "Tag is too long! (Max 511 characters)" end ply:SetNW2String("PlayerTag", tag) return ply:Nick() .. " changed his tag to: " .. tag --Notifies players of such change end) hook.Add("HUDDrawTargetID", "DrawTag", function() local target = LocalPlayer():GetEyeTrace().Entity if not IsValid(target) or not target:IsPlayer() then return end --If we are not looking at a player, do nothing local tag = target:GetNW2String("PlayerTag", "") if tag == "" then return end --If the player has no tag, do nothing --Displays tag after player name + health draw.SimpleText(tag, "TargetIDSmall", ScrW() / 2, ScrH() / 2 + 48, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) end)
OSZAR »