Play Sound When Animation Plays

Alright so I have a blacksmith who has an animation that loops while he hits the anvil with his hammer. I want a small sound effect to play when the animation loops. I would want more specifically for the sound to play at the end of the animation since thats when his hammer actually makes contact with the anvil, but either way is fine. Here’s the code that I have. It’s a script inside of my blacksmith. I’m also not getting any errors in my output.

local Sound = script.Parent.Sound --sound path

local Animation = script.Parent.Anim --animation path

local Humanoid = script.Parent.Humanoid --humanoid path

local AnimationTrack = Humanoid:LoadAnimation(Animation)

AnimationTrack:Play()

Sound:Play()

AnimationTrack.DidLoop:Wait()

Sound:Stop()

it should be like this

local Sound = script.Parent.Sound
local Animation = script.Parent.Anim
local Humanoid = script.Parent.Humanoid
local AnimationTrack = Humanoid.Animator:LoadAnimation(Animation)

AnimationTrack.Looped = false
AnimationTrack:Play()

AnimationTrack.Stopped:Connect(function()
	AnimationTrack:Play()
	
	coroutine.wrap(function()
		task.wait(0.02) -- delay
		
		Sound:Play()
	end)()
end)
1 Like