In order to locally mute another player with the new Audio API, you’ll need to reference that player’s AudioDeviceInput
and set its Muted
property to true
.
As an example:
-
Add a
TextBox
, aTextButton
, and aLocalScript
into the frame.- The
TextBox
would be where you can type the name of another player in the game - Afterwards, the
TextButton
would be pressed when a player wants to locally mute / unmute that player.
- The
-
In the
LocalScript
, we need to do the following things:-
Listen for the
TextButton
to be pressed with theActivated
event. -
Check if the
Text
that the player typed into theTextBox
is the name of another player that is currently in the game. -
If that player is in the game, check if there is an
AudioDeviceInput
that was created for that player (which will be added to the player object, by default, if the player has voice chat enabled and theVoiceChatService.EnableDefaultVoice
property is enabled. -
If an
AudioDeviceInput
was found, update itsMuted
property to the opposite value of what it currently is (so that if the player isn’t locally muted, it will mute them, but if they have already been muted, it will unmute them).
-
Here’s an example of what the code for the LocalScript
might look like:
local Players = game:GetService("Players")
local Frame = script.Parent
local TextBox = Frame:WaitForChild("TextBox")
local TextButton = Frame:WaitForChild("TextButton")
TextButton.Activated:Connect(function()
local Text = TextBox.Text
if Text == nil then return end
local lowercaseTextBoxText = string.lower(Text)
local player
for _, otherPlayer in Players:GetPlayers() do
local lowercasePlayerName = string.lower(otherPlayer.Name)
if lowercasePlayerName == lowercaseTextBoxText then
player = otherPlayer
break
end
end
if not player then return end
local AudioDeviceInput = player:FindFirstChildOfClass("AudioDeviceInput")
if not AudioDeviceInput then return end
local currentValue = AudioDeviceInput.Muted
AudioDeviceInput.Muted = not currentValue
end)