I’m trying to play an animation if/when noise (of any kind) comes from a player’s microphone. That’s all. Also where should this LocalScript be located? (StarterPlayerScripts, or StarterCharacterScripts)
Here’s my code, so far:
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
-- Wait for Humanoid and Animator
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
-- Load the talking animation
local TalkAnim = script:WaitForChild("TalkAnim")
local LoadedTalkAnim = Animator:LoadAnimation(TalkAnim)
local Input = Player.AudioDeviceInput
local AudioAnalyzer = Instance.new("AudioAnalyzer", Character)
local Wire = Instance.new("Wire", Character)
Wire.SourceInstance = Input
Wire.TargetInstance = AudioAnalyzer
AudioAnalyzer.???:Connect(function(Decibels)
if Decibels > 0 then
if not LoadedTalkAnim.IsPlaying then
LoadedTalkAnim:Play()
end
else
if LoadedTalkAnim.IsPlaying then
LoadedTalkAnim:Stop()
end
end
end)
AudioAnalyzer has PeakLevel for the recent loudest volume and RmsLevel for a more stable recent average volume. Neither of these properties are trackable with Changed like what your snippet tries to do. You’ll need to read these at whatever interval you prefer, like a Heartbeat signal.
Right now, your snippet doesn’t do any work to care about a dead player respawning so you can just place it in the character if you don’t want to implement that.
This is a testing script I did a while back… however turns out my mic wasn’t/isn’t working. Never did come back to finish this off, maybe you can…
--LocalScript in StarterCharacterScripts
local p = game:GetService("Players").LocalPlayer
local c = p.Character or p.CharacterAdded:Wait()
local a = c:WaitForChild("Humanoid"):WaitForChild("Animator")
:LoadAnimation(script:WaitForChild("TalkAnim"))
local d = Instance.new("AudioAnalyzer", c)
local w = Instance.new("Wire", c)
w.SourceInstance = p.AudioDeviceInput
w.TargetInstance = d
d.DecibelsChanged:Connect(function()
if d.Decibels > 0 then a:Play() else a:Stop() end
end)
Tried to make it fit your script… Keep in mind I was testing here, hardly a finished script.