help with sending a string to all clients on a server

The question pretty much says it all .
Can someone please give me an example on how to send a string to all clients via console command.

Why would you want to do it with a console command? Just use net or usermessages. I could be wrong with the usermessage code, since I haven’t used it since the net library was added, but you get the gist.

[lua]

–With net message:

if SERVER then
util.AddNetworkString( “teststring” )

net.Start( "teststring" )
net.WriteString( "hi" )
net.Broadcast() --Use net.Send( player ) to send to a specific player.

else
net.Receive( “teststring”, function( len )
print( net.ReadString() )
end )
end

–With usermessage:

if SERVER then
umsg.Start( “teststring” ) --There is a second argument for this(?), if you want to send it to a specific player. i.e. umsg.Start( str, ply )
umsg.String( “hi” )
umsg.End()
else
usermessage.Hook( “teststring”, function( um )
print( um:ReadString() )
end )
end
[/lua]

thanks . I got another question. will this work if I where to use this on [lua] surface.DrawText( string text ) [/lua]

so if i where to edit the string i would show up

Read gmod lua tutorials…

i have read them . i asked the second question because i don’t want to keep using SetNWString(). I already have enough on my server as is

Make a variable, and set a value to it when you receive the message. Then, use that variable to draw the text in whatever hook you’re using.

Here’s a shitty example:

[lua]
local str

usermessage.Hook( “MessageNameHere”, function( um )
str = um:ReadString()
end )

hook.Add( “HUDPaint”, “test”, function()
str = str ~= nil and str or “Not Received”
draw.SimpleText( str, “font”, ScrW() / 2, ScrH() / 2, color_white, 1 )
end )
[/lua]

nil is treated as false, you don’t need to over complicate things:
[lua]str = str or “[N/A]”[/lua]

I got one more question . Can you edit or overwrite [lua] umsg.String( ) [/lua] with Derma cause I keep getting [lua] attempt to index global ‘umsg’ (a nil value)[/lua]
when i attempt to overwrite it .

bump?

The umsg library can only be used on the server.

Don’t know why you would want to “overwrite” it anyway.

Im attempting to make a billboard system where the player goes to an npc and pays for advertisment. but I cant figure out how to change the text on the on the [lua] umsg.String( ) [/lua]. when the players submits the text. unless thers a better way. by the way Greetings thank you for helping me in all of this.

Maybe this will explain some more of what I am trying to do.

http://i.minus.com/jbinXtn09F1wyv.jpg

I want it so when the player hits the submit button it will change the text on the billboard and everyone can see it .
That is where I am stuck in.

Can you show us some code so we know what you did there.

cl_init.lua to the menu.

[lua]

function paintmenu()

local Panel = vgui.Create( “DFrame” )
Panel:SetPos( ScrW()- 1000,50 )
Panel:SetSize( 500, 500 )
Panel:SetTitle( “” )
Panel:SetVisible( true )
Panel:SetDraggable( true )
Panel:ShowCloseButton( true )
Panel:MakePopup()
Panel:SetSkin(“Surface”)

local PropertySheet = vgui.Create( “DPropertySheet” )
PropertySheet:SetParent( Panel )
PropertySheet:SetPos( 5, 30 )
PropertySheet:SetSize( 480, 415 )
PropertySheet:SetSkin(“Surface”)

PropertySheet.Paint = function()   
	PropertySheet:SetMouseInputEnabled(true)     
	surface.DrawOutlinedRect(0,0,PropertySheet:GetWide(),PropertySheet:GetTall() )
	draw.RoundedBox( 0, 0, 0, PropertySheet:GetWide() , PropertySheet:GetTall(), Color(250, 10, 10, 240))
end

local selectbillboard = vgui.Create( “DFrame” )
selectbillboard:SetTitle( “select” )
selectbillboard:SetVisible( true )
selectbillboard:SetDraggable( false )
selectbillboard:ShowCloseButton( false )
selectbillboard:SetSkin(“Surface”)

local DermaList = vgui.Create( “DPanelList”, selectbillboard )
DermaList:SetPos( 5,25 )
DermaList:SetSize( 450, 350 )
DermaList:SetSpacing( 5 )
DermaList:EnableHorizontal( false )
DermaList:EnableVerticalScrollbar( true )

local InfoLabel = vgui.Create(“DLabel”);
InfoLabel:SetColor(Color(255,255,255,255));
InfoLabel:SetFont(“font”);
InfoLabel:SetText("");
DermaList:AddItem( InfoLabel )

local billboard_1_texbox_title = vgui.Create( “DTextEntry” )
billboard_1_texbox_title:SetEnterAllowed( false )
DermaList:AddItem( billboard_1_texbox_title )

local InfoLabel = vgui.Create(“DLabel”);
InfoLabel:SetColor(Color(255,255,255,255));
InfoLabel:SetFont(“font”);
InfoLabel:SetText("");
DermaList:AddItem( InfoLabel )

local billboard_1_texbox_first_line = vgui.Create( “DTextEntry” )
billboard_1_texbox_first_line:SetEnterAllowed( false )
DermaList:AddItem( billboard_1_texbox_first_line )

local submit_Button = vgui.Create( “DButton” )
submit_Button:SetParent( DermaList )
submit_Button:SetText( “submit” )

submit_Button.DoClick = function (ply)

– This is the part where I dont know what to do in oder for the player to add the text on the billboard.
– so that everyone can see it
–local message = billboard_1_texbox_title:GetValue()

end

DermaList:AddItem( DermaButton ) 

PropertySheet:AddSheet( “billboard store”, selectbillboard, “icon16/application_edit.png”, false, false, “” )

end

usermessage.Hook(“OpenPaint”, paintmenu)
[/lua]

init.lua to the billboard entity

[lua]
AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )
include( “shared.lua” )

function ENT:Initialize( )
self:SetModel(“models/props/cs_assault/billboard.mdl”)
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
self:SetUseType(SIMPLE_USE)
self:SetNWString(“ad”,“title”)
self:SetNWString(“ad2”,“second line”)
local phys = self:GetPhysicsObject()
phys:Wake()

end

function ENT:SpawnFunction( ply, tr )

if ( !tr.Hit ) then return end
local SpawnPos = tr.HitPos + tr.HitNormal * 106

local ent = ents.Create( "day_night" )
ent:SetPos( SpawnPos )

ent:Spawn()
ent:Activate()
return ent

end

function ENT:OnRemove()
end

[/lua]

If someone could help me figure this is out. I would be very much appreciated

bump?

You must pass entity through your umsg to the panels.
Then, when you click you do:



net.Start( "teststring" )
	net.WriteEntity( ent ) -- Pass the entity from your usermessage
	net.WriteString( "line1" ) -- Pass the text here
	net.WriteString( "line2" )
net.SendToServer()


And then on your init.lua you add



util.AddNetworkString( "teststring" )
net.Receive( "teststring", function( len )
	local ent = net.ReadEntity()
	
	ent:SetNWString("ad",net.ReadString()) 
	ent:SetNWString("ad2",net.ReadString())
end )


I did what you said . I didn’t get any errors but i don’t see any text on the billboard

[editline]2nd June 2013[/editline]

I uploaded what i have so far to see if anyone else want to have ago at it

http://upit.us/s7/npc_billboard.zip

sorry if it looks bad