Voice chat only in radius, all teams can hear each other. (Need modification to a script I have provided in this thread)


hook.Add("PlayerCanHearPlayersVoice" , "Wat" , function( p1 , p2 )    
    return (p1:GetPos():Distance(p2:GetPos()) <= 2000)   
end )   


This script was made for me a while ago. It allows players to only hear each other within 2000 units of each other when speaking via microphone.

This works well for sandbox, because when sv_alltalk is 0, the script is working, and when sv_alltalk is 1, all players on the server can hear each other from anywhere else. But a friend of mine is trying to use it in a gamemode of his and wants the different teams to be able to hear each other when they’re close enough; when sv_alltalk is 0, the script does work, but only members of the same team can hear each other within a radius while the enemy team can’t be heard no matter what.

Therefore, I think I’m just asking for a little addition to this script that makes it so all teams can hear each other regardless of sv_alltalk setting or something. I wouldn’t know how to do it, but if someone does, that really would be great. Thanks for reading, hope to hear from anybody who’s interested soon.

[lua]hook.Add(“PlayerCanHearPlayersVoice” , “Wat” , function( p1 , p2 )
if p1:Team() == p2:Team() then
return true, true – Second true = voice radius fading
else
return false
end
end )[/lua]

If you dont like the inbuilt voice fading then use this.

[lua]hook.Add(“PlayerCanHearPlayersVoice” , “Wat” , function( p1 , p2 )
if p1:Team() == p2:Team() and p1:GetPos():Distance( p2:GetPos() ) <= 2000 then
return true
else
return false
end
end )[/lua]

You must keep sv_alltalk 0, because setting it to one causes this hook to no longer get called.

Wait up, fading? Like when you get further away, the voices are quieter?

Is there a way you could add that into the original script, but keep the teams part out? It doesn’t have the fading out function, if that is what you meant by voice fading. I would like it for my sandbox server.

Thank you so much for this script. I’ll pass this on to him; you already have his thanks.

Exactly.

[lua]local sv_alltalk2 = CreateConVar( “sv_alltalk2”, “1”, FCVAR_NOTIFY )
local sv_rangedtalk = CreateConVar( “sv_rangedtalk”, “0”, FCVAR_NOTIFY )
local sv_voiceenable = GetConVar( “sv_voiceenable” )

hook.Add( “PlayerCanHearPlayersVoice”, “Talking”, function( lsnr, tlkr )

if !sv_voiceenable:GetBool() then
	return false
elseif sv_alltalk2:GetBool() or ( !sv_alltalk2:GetBool() and lsnr:Team() == tlkr:Team() ) then
	return true, sv_rangedtalk:GetBool()
end

end )[/lua]

There. Now you can fully control everything with cvars. Let me know if something doesn’t work right.

Thank you so much! I’ll test it as soon as I can.