How to send a string from the server to a specific client?
6 replies, posted
Thanks to code_gs I have the !timeleft command sorted, but I don't yet have the answer for any other form of message from the server to the player (e.g. gamemode messages being sent as a notification instead of a chat message).
To clarify, what I'm looking to do is have a function which can be used to put a string in chat (ex: player just killed you!), which can then be sent to a specific player and then to have the client show that as a notification ala the undo notification. So far I have this, and I see no reason as to why it isn't working. Come on facepunch, make me feel real sad about my skill (or lack of) in lua.
Clientside:
[CODE]net.Receive("Notification", function( len,ply )
notification.AddLegacy( str, NOTIFY_GENERIC, 1 )
surface.PlaySound( "buttons/button15.wav" )
end )
[/CODE]
Serverside:
[CODE]function Notify( str )
net.Start( "Notification" )
net.WriteString( string "str" )
net.Send( Entity(1) )
end
function msg.Send( ply, str )
ply:Notify(str)
end
[/CODE]
Your code doesn't really make any sense. Firstly, there's no need to create special global functions for this sort of thing -- you can just embed the code in the primary function. You also should be using hooks instead of overriding GAMEMODE functions. Also, if you are supporting three different command strings, you can just use a table lookup for comparrisons. Lastly, using an OnPlayerChat hook will prevent any networking from having to be done since it's all clientside.
Try this:
[code]local timeleft = {
["timeleft"] = true,
["!timeleft"] = true,
["/timeleft"] = true
}
hook.Add( "OnPlayerChat", "CoopaTroopa-Notification", function( ply, text )
if ( timeleft[text:lower()] ) then
-- Only print if the local client said it, but always block it
if ( ply == LocalPlayer() ) then
notification.AddLegacy( string.ToMinutesSeconds(GetTimeLeft()).." left of current round, "..GetRoundsLeft().." remaining rounds.", NOTIFY_GENERIC, 1 )
surface.PlaySound( "buttons/button15.wav" )
end
return ""
end
end )[/code]
To be honest, I was originally planning to have it so I could send other strings, as a form of replacement for the server putting stuff into chat, which is part of the reason it looks so incredibly poor.
That being said, thanks a lot, that makes a lot more sense than what i was doing
Sorry, but I'm going to bump instead of starting another thread. Should be alright, according to the rules.
why are you bumping it if someone already gave you an answer?
[editline]3rd January 2017[/editline]
I would've said the same thing by the way - it's much simpler and doesn't send unnecessary net messages
They've given a solution to an issue, but they haven't answered the question. I'm still glad he answered, but I still don't know how to make a server send a specific string to a specific player.
I think your original code would work but it needs a few changes:
[CODE]
// CLIENTSIDE
// This code works with both serverside examples
net.Receive( 'Notification', function( len, ply )
local str = net.ReadString()
notification.AddLegacy( str, NOTIFY_GENERIC, 1 )
surface.PlaySound( "buttons/button15.wav" )
end )
[/CODE]
[CODE]
// SERVERSIDE
util.AddNetworkString( 'Notification' )
local msg = {}
msg.Send = function( ply, str )
net.Start( 'Notification' )
net.WriteString( str )
net.Send( ply )
end
// USAGE EXAMPLE:
// local ply = Entity(1)
// msg.Send( ply, "TEST STRING" )
[/CODE]
But please use the way that other guy posted before, it's much simpler and doesn't do pointless networking
[editline]3rd January 2017[/editline]
And if you want to add the 'Notify' function (or any other function) to the player metatable you have to use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/FindMetaTable]FindMetaTable[/url], e.g.
[CODE]
// SERVERSIDE
util.AddNetworkString( 'Notification' )
local meta = FindMetaTable( "Player" )
function meta:Notify( str )
net.Start( 'Notification' )
net.WriteString( str )
net.Send( self )
end
// USAGE EXAMPLE:
// local ply = Entity(1)
// ply:Notify( "TEST STRING" )
[/CODE]
But there's no reason to make it so unnecessarily complicated when you can just use one hook
Sorry, you need to Log In to post a reply to this thread.