I tried to write a little something for my server so that there can be team voice chat by pressing z. But i cant seem to get it to work. heres what i got.
[lua]
local sv_alltalk = GetConVar( “sv_alltalk” )
function GM:PlayerCanHearPlayersVoice( pListener, pTalker )
local alltalk = sv_alltalk:GetInt()
if ( key == KEY_Z ) then
sv_alltalk = 1
return pListener:Team() == pTalker:Team(), false
KeyPress isn’t always defined. Normally I’d suggest PlayerBindPress but it’d require networking… You could use the voice start / end hooks to enable / disable team voice ( if on start x button held then x chat opens )
You’d still need to network, but these are the hooks for voice.
PlayerCanHear is server-side so it’d need to read something that was networked. The Start/EndVoice hooks are clientside, those start/stop when someone is talking. Take a look at my dev base to see what I did to let _p:IsSpeaking( ) work serverside…
It’s pretty straight-forward… Network a value when voice starts / stops, add IsSpeaking to the server then hook.Add PlayerCanHear and check for IsSpeaking ( also check for the team / team-specific stuff and use one of the IN_ keys; if you don’t want to use an IN_ key, use the clientside voice start hook to check for the voice only key and if it is pressed send another true/false var for team only chat )…
Doing it that way though may cause issues because the voice-record button would still need to be used and the modifier key would need to be sent with / without it )…
Another way is to use PlayerBindPress to detect bound buttons, check for a certain key and network when it is pushed / released ( that way if _p:IsSpeaking( ) && _p.TeamOnlyVoice = true … then it is voice ) so player can go from being in all voice to team voice by pressing a button. You could also toggle it, on press set self.TeamOnlyVoice = !self.TeamOnlyVoice and draw an icon on the hud if it is on…
In that example I read the key and turn it into a string. You can also look for the specific bind used by reading the _bind var by itself instead of putting it through the conversion…
There are other methods of checking key press in case it isn’t bound but the KeyPress hooks are fairly bad and don’t always fire ( working on something to fix all of the issues ) which is why PlayerBindPress is a good way.
You could also use a movement hook which passes the command through which allows you to grab keys pressed but try with PlayerBindPress…
OnPress toggle ( network to server each press and maintain the variable LocalPlayer( ).VoiceToggle = !LocalPlayer( ).VoiceToggle on client, and on server use the net.Receive( “name”, function( _len, _p ) _p.VoiceToggle = !_p.VoiceToggle; end ); and they should stay in sync…
Then in your CanHear hook you’d simply check if _p.VoiceToggle is set, if it is then only allow team members to hear.