How to add sound play on animation?

I have a custom emote in my game and I also have audio for the emote, I need help writing a script that will play audio coming from the character as long as the animation is running, and stop when the animation stops, I want all players to hear this music, how can I do it? Heres the script im using

local keyPressed = Enum.KeyCode.P --Another example Enum.KeyCode.N

local animationId = “8999377853” --Change to your animation Id

local userInputService = game:GetService(“UserInputService”)

local player = game:GetService(“Players”).LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()

local humanoid = character:WaitForChild(“Humanoid”)

local animation = Instance.new(“Animation”)

animation.AnimationId = “rbxassetid://”…animationId

local animationTrack = humanoid:LoadAnimation(animation)

userInputService.InputBegan:Connect(function(input,isTyping)

if isTyping then return end

if input.KeyCode == keyPressed then

if not animationTrack.IsPlaying then

animationTrack:Play()

else

animationTrack:Stop()

end

end

end)

Add a sound into workspace or wherever you want. then simple get the sound in a variable. Ex: local sound = game.workspace.Sound

now when the animation plays, sound:Play()
and when it stops, sound:Stop()

2 Likes

Thanks but i have no clue on how exactly to do this. i got the script from a youtube video, im not a scripter myself.

do u know how to get the sound ID and add a sound into your explorer?

how do you add distance to this sound? I tried to reduce RollOffmaxDistance but it doesn’t work…