This event fires whenever the
AnimationController begins playing an animation. It returns the
AnimationTrack can be used to access the animation’s playback functions and events. It will only fire for animations playing on the specific
Humanoid/AnimationPlayed for the
Humanoid variant of this function.
Listen For Animation Effects
The function in the following code sample provides an example of how the AnimationTrack.KeyframeReached event can be used to link different effects to an animation.
The function listens for any animations being played on the Humanoid (or AnimationController), and when a new animation is played listens for a keyframe named ‘Effect’ being reached. When a keyframe named ‘Effect’ is reached some basic particles will be emitted. Once the animation has stopped the connections used by the function are disconnected, in line with best practice to avoid memory leaks.
Although the particle effect in this sample is very simple, it could be substituted for a range of more complicated effects using the Sound, Beam, ParticleEffect or other objects. Additionally, in many cases the developer may only care about effects on one specific animation (rather than every animation played on a Humanoid). In this case, only the KeyframeReached portion of this sample would be needed.
local Debris = game:GetService("Debris") local function listenForAnimationEffects(humanoid) -- would also work for an AnimationController -- listen for new animations being played on the Humanoid humanoid.AnimationPlayed:Connect(function(animationTrack) local keyframeConnection = nil -- listen for the 'Effect' keyframe being reached keyframeConnection = animationTrack.KeyframeReached:Connect(function(keyframeName) if keyframeName == "Effect" then -- make sure the Humanoid RootPart exists if humanoid.RootPart then -- create a basic particle effect local particles = Instance.new("ParticleEmitter", humanoid.RootPart) particles.Rate = 0 particles:Emit(10) Debris:AddItem(particles, 2) end end end) local stoppedConnection = nil stoppedConnection = animationTrack.Stopped:Connect(function() -- clean up old connections to stop memory leaks keyframeConnection:Disconnect() stoppedConnection:Disconnect() end) end) end