how would I mute a player until they reach a certain stats for ex time played
Muting a player is as simple as using the Property AudioDeviceInput.Muted.
you’d need to check if a player reaches a certain stat (im assuming a leaderstat in this scenario) and then mute them.
Here’s a simple stat code setup to your use case:
local Players = game:GetService("Players")
local valueToMeet = 0 -- This will be the value that will determine whether the player should be muted or not
Players.PlayerAdded:Connect(function(player: Player)
local audioInput: AudioDeviceInput = player:WaitForChild("AudioDeviceInput") -- We will use this to mute the player using AudioDeviceInput's muted property
local ls = Instance.new("Folder")
ls.Name = "leaderstats"
ls.Parent = player
local stat = Instance.new("IntValue") -- set this to whatever type of stat you want
stat.Value = nil -- Default Value
stat.Parent = ls
end)
The script above creates a leaderstats folder, parents it to the player, and then a stat folder which parents to leaderstats.
From there, you can check if the stat meets a condition, which in this case if they reach a certain value.
This can be done with detecting if the stat’s value property has changed and using an if statement to check if its a single value.
We will use Changed Event to detect when the stat’s value changes. The Changed event passes the new value of the stat, which we can use to check if the new stat meets the value you set it to.
stat.Changed:Connect(function(newValue) -- Event that detects when the stat value has changed
if stat == valueToMeet then
audioInput.Muted = true
end
end)
I hope I helped. Let me know if you run into any problems or if you have any questions
Is this instance created in the player object or is it a player property? I am asking this cause I never saw a audiodeviceinput instance in the player object
That’s why we use WaitForChild
as it’ll yield until AudioDeviceInput is found under the player which, from what I understand, happens when the player joins.
Could you link me the wiki for this bc this AudioDeviceInput is completely new to me
Sure. Here you go:
https://create.roblox.com/docs/reference/engine/classes/AudioDeviceInput
Here’s a link to the muted property:
https://create.roblox.com/docs/reference/engine/classes/AudioDeviceInput#Muted
Would this work for the chat as well?
Nope. You’d need to use TextChatService in that case. Below the code from script made by a Roblox Staff Member that contains a function you can use the mute the player.
The script below mutes any “speakers” in chat by checking if the speaker’s UserID matches the User ID of the userId provided. In your case, you’d pass player.UserId
as the argument for muteUserId
when checking if the stat meets a value from the script I provided earlier.
local TextChatService = game:GetService("TextChatService")
local function muteUserId(mutedUserId)
-- listen for future TextSources
TextChatService.DescendantAdded:Connect(function(child)
if child:IsA("TextSource") then
if child.UserId == mutedUserId then
child.CanSend = false
end
end
end)
-- mute any current TextSources
for _, child in TextChatService:GetDescendants() do
if child:IsA("TextSource") then
if child.UserId == mutedUserId then
child.CanSend = false
end
end
end
end
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.