AnimationTrack

Show Deprecated
Not Creatable

Controls the playback of an animation on a Humanoid or AnimationController. This object cannot be created, instead it is returned by the Humanoid:LoadAnimation() method.

Code Samples

Animation Creation

1local Players = game:GetService("Players")
2
3local player = Players:FindFirstChild("Builderman")
4
5local character = player.Character
6local humanoid = character:FindFirstChild("Humanoid")
7
8local animation = Instance.new("Animation")
9animation.AnimationId = "http://www.roblox.com/asset/?id=507771019" -- Roblox dance emote
10
11local animationTrack = humanoid:LoadAnimation(animation)
12animationTrack:Play()

Summary

Properties

The Animation object that was used to create this AnimationTrack.

READ ONLY
NOT REPLICATED

A read only property that returns true when the AnimationTrack is playing.

READ ONLY
NOT REPLICATED

A read only property that returns the length (in seconds) of an AnimationTrack. This will return 0 until the animation has fully loaded and thus may not be immediately available.

READ ONLY
NOT REPLICATED

Sets whether the animation will repeat after finishing. If it is changed while playing the result will take effect after the animation finishes.

Sets the priority of an AnimationTrack. Depending on what this is set to, playing multiple animations at once will look to this property to figure out which Keyframe Poses should be played over one another.

The Speed of an AnimationTrack is a read only property that gives the current playback speed of the AnimationTrack. This has a default value of 1. When speed is equal to 1, the amount of time an animation takes to complete is equal to AnimationTrack.Length (in seconds).

READ ONLY
NOT REPLICATED

Returns the position in time in seconds that an AnimationTrack is through playing its source animation. Can be set to make the track jump to a specific moment in the animation.

NOT REPLICATED

Read-only property that gives the current weight of the AnimationTrack. It has a default value of 1.

READ ONLY
NOT REPLICATED

Read-only property that gives the current weight of the AnimationTrack.

READ ONLY
NOT REPLICATED

Events


Fires when an AnimationTrack loops on the next update following the end of the previous animation loop.


Fires every time playback of an AnimationTrack reaches a Keyframe that does not have the default name - "Keyframe.".


Fires whenever the AnimationTrack finishes playing.

Methods

AdjustSpeed(speed: number): void  

Changes the AnimationTrack.Speed of an animation. A positive value for speed plays the animation forward, a negative one plays it backwards, and 0 pauses it.

AdjustWeight(weight: number, fadeTime: number): void  

Changes the weight of an animation, with the optional fadeTime parameter determining how long it takes for AnimationTrack.WeightCurrent to reach AnimationTrack.WeightTarget.


Returns an event that fires when a specified KeyframeMarker has been hit in an animation.

GetTimeOfKeyframe(keyframeName: string): number  

Returns the time position of the first Keyframe of the given name in an AnimationTrack.

Play(fadeTime: number, weight: number, speed: number): void  

Plays the AnimationTrack. Once called an AnimationTrack will play with the specified fadeTime, weight and speed.

Stop(fadeTime: number): void  

Stops the AnimationTrack. Once called playback of the AnimationTrack will stop and the weight of the animation will move towards zero over a length of time specified by the optional fadeTime parameter.

Properties

Animation

Read Only
Not Replicated

The Animation object that was used to create this AnimationTrack. To create an AnimationTrack the developer must load an Animation object onto a Humanoid or AnimationController using the Humanoid:LoadAnimation() method.

The Animation property is used to identify the underlying Animation of an AnimationTrack.

IsPlaying

Read Only
Not Replicated

A read only property that returns true when the AnimationTrack is playing.

This property can be used by developers to check if an animation is already playing before playing it (as that would cause it to restart). If a developer wishes to obtain all playing AnimationTracks on a Humanoid or AnimationController they should use Humanoid:GetPlayingAnimationTracks()

Length

Read Only
Not Replicated

A read only property that returns the length (in seconds) of an AnimationTrack. This will return 0 until the animation has fully loaded and thus may not be immediately available.

When the AnimationTrack.Speed of an AnimationTrack is equal to 1, the animation will take AnimationTrack.Length (in seconds) to complete.

Looped

This property sets whether the animation will repeat after finishing. If it is changed while playing the result will take effect after the animation finishes.

The Looped property for AnimationTrack defaults to how it was set in the animation editor. However this property can be changed, allowing control over the AnimationTrack while the game is running. Looped also correctly handles animations played in reverse (negative AnimationTrack.Speed). After the first keyframe is reached, it will restart at the last keyframe.

This property allows the developer to have a looping and non looping variant of the same animation, without needing to upload two versions to Roblox.

This property sets the priority of an AnimationTrack. Depending on what this is set to, playing multiple animations at once will look to this property to figure out which Keyframe Poses should be played over one another.

The Priority property for AnimationTrack defaults to how it was set and published from Studio's Animation Editor. It uses the AnimationPriority Enum, which has 7 priority levels.

  1. Core (lowest priority)
  2. Idle
  3. Movement
  4. Action
  5. Action2
  6. Action3
  7. Action4 (highest priority)

Correctly set animation priorities, either through the editor or through this property allow multiple animations to be played without them clashing. Where two playing animations direct the target to move the same limb in different ways, the AnimationTrack with the highest priority will show. If both animations have the same priority, the weights of the tracks will be used to combine the animations.

This property also allows the developer to play the same animation at different priorities, without needing to upload additional versions to Roblox.

Speed

Read Only
Not Replicated

The Speed of an AnimationTrack is a read only property that gives the current playback speed of the AnimationTrack. This has a default value of 1. When speed is equal to 1, the amount of time an animation takes to complete is equal to AnimationTrack.Length (in seconds).

If the speed is adjusted, then the actual time it will take a track to play can be computed by dividing the length by the speed. Speed is a unitless quantity.

Speed can be used to link the length of an animation to different game events (for example recharging an ability) without having to upload different variants of the same animation.

This property is read only, and you can change it using AnimationTrack:AdjustSpeed().

TimePosition

Not Replicated

Returns the position in time in seconds that an AnimationTrack is through playing its source animation. Can be set to make the track jump to a specific moment in the animation.

TimePosition can be set to go to a specific point in the animation, but the AnimationTrack must be playing to do so. It can also be used in combination with AnimationTrack:AdjustSpeed() to freeze the animation at a desired point (by setting speed to 0).

WeightCurrent

Read Only
Not Replicated

When weight is set in an AnimationTrack it does not change instantaneously but moves from WeightCurrent to AnimationTrack.WeightTarget. The time it takes to do this is determined by the fadeTime parameter given when the animation is played, or the weight is adjusted.

WeightCurrent can be checked against AnimationTrack.WeightTarget to see if the desired weight has been reached. Note that these values should not be checked for equality with the == operator, as both of these values are floats. To see if WeightCurrent has reached the target weight, it is recommended to see if the distance between those values is sufficiently small (see code sample below).

The animation weighting system is used to determine how AnimationTracks playing at the same priority are blended together. The default weight is one, and no movement will be visible on an AnimationTrack with a weight of zero. The pose that is shown at any point in time is determined by the weighted average of all the Poses and the WeightCurrent of each AnimationTrack. In most cases blending animations is not required and using AnimationTrack.Priority is more suitable.

WeightTarget

Read Only
Not Replicated

AnimationTrack.WeightTarget is a read-only property that gives the current weight of the AnimationTrack. It has a default value of 1 and is set when AnimationTrack:Play(), AnimationTrack:Stop() or AnimationTrack:AdjustWeight() is called. When weight is set in an AnimationTrack it does not change instantaneously but moves from WeightCurrent to AnimationTrack.WeightTarget. The time it takes to do this is determined by the fadeTime parameter given when the animation is played, or the weight is adjusted.

WeightCurrent can be checked against AnimationTrack.WeightTarget to see if the desired weight has been reached. Note that these values should not be checked for equality with the == operator, as both of these values are floats. To see if WeightCurrent has reached the target weight, it is recommended to see if the distance between those values is sufficiently small (see code sample below).

The animation weighting system is used to determine how AnimationTracks playing at the same priority are blended together. The default weight is one, and no movement will be visible on an AnimationTrack with a weight of zero. The pose that is shown at any point in time is determined by the weighted average of all the Poses and the WeightCurrent of each AnimationTrack. In most cases blending animations is not required and using AnimationTrack.Priority is more suitable.

Events

DidLoop

This event fires whenever a looped AnimationTrack completes a loop, on the next update.

Currently it may also fire at the exact end of a non looped animation track but this behavior should not be relied upon.


Code Samples

Play AnimationTrack for a Number of Loops

1local function playForNumberLoops(animationTrack, number)
2 animationTrack.Looped = true
3 animationTrack:Play()
4 local numberOfLoops = 0
5 local connection = nil
6 connection = animationTrack.DidLoop:Connect(function()
7 numberOfLoops = numberOfLoops + 1
8 print("loop: ", numberOfLoops)
9 if numberOfLoops >= number then
10 animationTrack:Stop()
11 connection:Disconnect() -- it's important to disconnect connections when they are no longer needed
12 end
13 end)
14end
15
16local animation = Instance.new("Animation")
17animation.AnimationId = "rbxassetid://507765644"
18
19local humanoid = script.Parent:WaitForChild("Humanoid")
20local animator = humanoid:WaitForChild("Animator")
21local animationTrack = animator:LoadAnimation(animation)
22
23playForNumberLoops(animationTrack, 5)

Ended


KeyframeReached

Fires every time playback of an AnimationTrack reaches a Keyframe that does not have the default name - "Keyframe."

This event allows a developer to run code at predefined points in an animation (set by Keyframe names). This allows the default functionality of Roblox animations to be expanded upon by adding Sounds or ParticleEffects at different points in an animation.

Keyframe names do not need to be unique. For example, if an Animation has three keyframes named "Particles" the KeyframeReached event will fire each time one of these keyframes is reached.

Keyframe names can be set in the Roblox Animation Editor when creating or editing an animation. They cannot however be set by a Script on an existing animation prior to playing it.

Parameters

keyframeName: string

The name of the Keyframe reached.


Code Samples

Listen For Animation Effects

1local function listenForAnimationEffects(humanoid) -- would also work for an AnimationController
2 -- listen for new animations being played on the Humanoid
3 humanoid.AnimationPlayed:Connect(function(animationTrack)
4 local keyframeConnection = nil
5 -- listen for the 'Effect' keyframe being reached
6 keyframeConnection = animationTrack.KeyframeReached:Connect(function(keyframeName)
7 if keyframeName == "Effect" then
8 -- make sure the Humanoid RootPart exists
9 if humanoid.RootPart then
10 -- create a basic particle effect
11 local particles = Instance.new("ParticleEmitter")
12 particles.Parent = humanoid.RootPart
13 particles.Rate = 0
14 particles:Emit(10)
15 task.delay(2, function()
16 if particles then
17 particles:Destroy()
18 end
19 end)
20 end
21 end
22 end)
23 local stoppedConnection = nil
24 stoppedConnection = animationTrack.Stopped:Connect(function()
25 -- clean up old connections to stop memory leaks
26 keyframeConnection:Disconnect()
27 stoppedConnection:Disconnect()
28 end)
29 end)
30end
31
32local humanoid = script.Parent:WaitForChild("Humanoid")
33
34listenForAnimationEffects(humanoid)
AnimationTrack KeyframeReached

1local Players = game:GetService("Players")
2
3local player = Players:GetChildren()[1]
4local character = workspace:WaitForChild(player.Name)
5local humanoid = character:WaitForChild("Humanoid")
6
7local animation = Instance.new("Animation")
8animation.AnimationId = "rbxassetid://437855404"
9local animTrack = humanoid:LoadAnimation(animation)
10
11animTrack.KeyframeReached:Connect(function(keyframeName)
12 print("Keyframe reached:" .. keyframeName)
13end)
14animTrack:Play()

Stopped

Fires whenever the AnimationTrack finishes playing.

This event has a number of uses. It can be used to wait until an AnimationTrack has stopped before continuing (for example, if chaining a series of animations to play after each other). It can also be used to clean up any Instances created during the animation playback.


Code Samples

AnimationTrack Stopped

1local function yieldPlayAnimation(animationTrack, fadeTime, weight, speed)
2 animationTrack:Play(fadeTime, weight, speed)
3 animationTrack.Stopped:Wait()
4 print("Animation has stopped")
5end
6
7local animation = Instance.new("Animation")
8animation.AnimationId = "rbxassetid://507765644"
9
10local humanoid = script.Parent:WaitForChild("Humanoid")
11local animator = humanoid:WaitForChild("Animator")
12local animationTrack = animator:LoadAnimation(animation)
13
14yieldPlayAnimation(animationTrack, 1, 0.6, 1)

Methods

AdjustSpeed

void

This function changes the AnimationTrack.Speed of an animation. A positive value for speed plays the animation forward, a negative one plays it backwards, and 0 pauses it.

An AnimationTrack's initial speed is set as a parameter in AnimationTrack:Play(). However a track's Speed can be changed during playback, using AdjustSpeed. When speed is equal to 1, the amount of time an animation takes to complete is equal to AnimationTrack.Length (in seconds).

When is adjusted, then the actual time it will take a track to play can be computed by dividing the length by the speed. Speed is a unitless quantity.

Speed can be used to link the length of an animation to different gameplay events (for example recharging an ability) without having to upload different variants of the same animation.

Parameters

speed: number

The playback speed the animation is to be changed to.

Default Value: "1"

Returns

void

Code Samples

Animation Speed

1local ContentProvider = game:GetService("ContentProvider")
2local Players = game:GetService("Players")
3
4local localPlayer = Players.LocalPlayer
5while not localPlayer.Character do
6 task.wait()
7end
8local character = localPlayer.Character
9local humanoid = character:WaitForChild("Humanoid")
10local animator = humanoid:WaitForChild("Animator")
11local animation = Instance.new("Animation")
12animation.AnimationId = "rbxassetid://507770453"
13
14ContentProvider:PreloadAsync({ animation })
15
16local animationTrack = animator:LoadAnimation(animation)
17
18local normalSpeedTime = animationTrack.Length / animationTrack.Speed
19animationTrack:AdjustSpeed(3)
20local fastSpeedTime = animationTrack.Length / animationTrack.Speed
21
22print("At normal speed the animation will play for", normalSpeedTime, "seconds")
23print("At 3x speed the animation will play for", fastSpeedTime, "seconds")
Playing Animation for a Specific Duration

1local function playAnimationForDuration(animationTrack, duration)
2 local speed = animationTrack.Length / duration
3 animationTrack:AdjustSpeed(speed)
4 animationTrack:Play()
5end
6
7local animation = Instance.new("Animation")
8animation.AnimationId = "rbxassetid://507765000"
9
10local humanoid = script.Parent:WaitForChild("Humanoid")
11local animator = humanoid:WaitForChild("Animator")
12
13local animationTrack = animator:LoadAnimation(animation)
14
15playAnimationForDuration(animationTrack, 3)

AdjustWeight

void

Changes the weight of an animation, with the optional fadeTime parameter determining how long it takes for AnimationTrack.WeightCurrent to reach AnimationTrack.WeightTarget.

When weight is set in an AnimationTrack it does not change instantaneously but moves from WeightCurrent to AnimationTrack.WeightTarget. The time it takes to do this is determined by the fadeTime parameter given when the animation is played, or the weight is adjusted.

WeightCurrent can be checked against AnimationTrack.WeightTarget to see if the desired weight has been reached. Note that these values should not be checked for equality with the == operator, as both of these values are floats. To see if WeightCurrent has reached the target weight, it is recommended to see if the distance between those values is sufficiently small (see code sample below).

The animation weighting system is used to determine how AnimationTracks playing at the same priority are blended together. The default weight is one, and no movement will be visible on an AnimationTrack with a weight of zero. The pose that is shown at any point in time is determined by the weighted average of all the Poses and the WeightCurrent of each AnimationTrack. See below for an example of animation blending in practice. In most cases blending animations is not required and using AnimationTrack.Priority is more suitable.

Parameters

weight: number

The weight the animation is to be changed to.

Default Value: "1"
fadeTime: number

The duration of time that the animation will fade between the old weight and the new weight for.

Default Value: "0.100000001"

Returns

void

Code Samples

AnimationTrack Change Weight

1local function changeWeight(animationTrack, weight, fadeTime)
2 animationTrack:AdjustWeight(weight, fadeTime)
3 local startTime = tick()
4 while math.abs(animationTrack.WeightCurrent - weight) > 0.001 do
5 task.wait()
6 end
7 print("Time taken to change weight " .. tostring(tick() - startTime))
8end
9
10local animation = Instance.new("Animation")
11animation.AnimationId = "rbxassetid://507765644"
12
13local humanoid = script.Parent:WaitForChild("Humanoid")
14local animator = humanoid:WaitForChild("Animator")
15local animationTrack = animator:LoadAnimation(animation)
16
17changeWeight(animationTrack, 0.6, 1)

GetMarkerReachedSignal

This function returns an event similar to the AnimationTrack.KeyframeReached event, except it only fires when a specified KeyframeMarker has been hit in an animation. The difference allows for greater control of when the event will fire.

To learn more about using this function, see Animation Events in the Animation Editor article.

More About Keyframes

Keyframe names can be set in the Roblox Animation Editor when creating or editing an animation. They cannot, however, be set by a Script on an existing animation prior to playing it.

Keyframe names do not need to be unique. For example, if an Animation has three keyframes named "EmitParticles," the connected event returned by this function will fire each time one of these keyframes is reached.

See also:

Parameters

name: string

The name of the KeyFrameMarker the signal is being created for.


Returns

The signal created and fired when the animation reaches the created KeyFrameMarker.

Code Samples

Listening to Keyframe Markers

1local Players = game:GetService("Players")
2
3local player = Players.LocalPlayer
4local character = player.Character or player.Character:Wait()
5local humanoid = character:WaitForChild("Humanoid")
6
7-- Create new "Animation" instance
8local kickAnimation = Instance.new("Animation")
9-- Set its "AnimationId" to the corresponding animation asset ID
10kickAnimation.AnimationId = "rbxassetid://2515090838"
11
12-- Load animation onto the humanoid
13local kickAnimationTrack = humanoid:LoadAnimation(kickAnimation)
14
15-- Play animation track
16kickAnimationTrack:Play()
17
18-- If a named event was defined for the animation, connect it to "GetMarkerReachedSignal()"
19kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString)
20 print(paramString)
21end)

GetTimeOfKeyframe

Returns the time position of the first Keyframe of the given name in an AnimationTrack. If multiple Keyframes share the same name, it will return the earliest one in the animation.

This function will return an error if it is uses with an invalid keyframe name (one that does not exist for example) or if the underlying Animation has not yet loaded. To address this make sure only correct keyframe names are used and the animation has loaded before calling this function.

To check if the animation has loaded, verify that the AnimationTrack's AnimationTrack.Length is greater than zero.

Parameters

keyframeName: string

The name associated with the Keyframe to be found.


Returns

The time, in seconds, the Keyframe occurs at normal playback speed.

Code Samples

Jump To Keyframe

1local function jumpToKeyframe(animationTrack, keyframeName)
2 local timePosition = animationTrack:GetTimeOfKeyframe(keyframeName)
3 if not animationTrack.IsPlaying then
4 animationTrack:Play()
5 end
6 animationTrack.TimePosition = timePosition
7end
8
9local ANIMATION_ID = 0
10local KEYFRAME_NAME = "Test"
11
12local animation = Instance.new("Animation")
13animation.AnimationId = "rbxassetid://" .. ANIMATION_ID
14
15local humanoid = script.Parent:WaitForChild("Humanoid")
16local animator = humanoid:WaitForChild("Animator")
17local animationTrack = animator:LoadAnimation(animation)
18
19jumpToKeyframe(animationTrack, KEYFRAME_NAME)

Play

void

When AnimationTrack:Play() is called the track's animation will begin playing and the weight of the animation will increase from 0 to the specified weight (defaults to 1) over the specified fadeTime (defaults to 0.1).

The speed the AnimationTrack will play at is determined by the speed parameter (defaults to 1). When the speed is equal to 1 the number of seconds the track will take to complete is equal to the track's AnimationTrack.Length property. For example, a speed of 2 will cause the track to play twice as fast.

The weight and speed of the animation can also be changed after the animation has begun playing by using the AnimationTrack:AdjustWeight() and AnimationTrack:AdjustSpeed() methods.

If the developer wants to start the animation at a specific point using AnimationTrack.TimePosition, it is important the animation is played before this is done.

Parameters

fadeTime: number

The duration of time that the animation's weight should be faded in for.

Default Value: "0.100000001"
weight: number

The weight the animation is to be played at.

Default Value: "1"
speed: number

The playback speed of the animation.

Default Value: "1"

Returns

void

Code Samples

Freeze Animation at Position

1function freezeAnimationAtTime(animationTrack, timePosition)
2 -- Set the speed to 0 to freeze the animation
3 animationTrack:AdjustSpeed(0)
4 if not animationTrack.IsPlaying then
5 -- Play the animation if it is not playing
6 animationTrack:Play()
7 end
8 -- Jump to the desired TimePosition
9 animationTrack.TimePosition = timePosition
10end
11
12function freezeAnimationAtPercent(animationTrack, percentagePosition)
13 -- Set the speed to 0 to freeze the animation
14 animationTrack:AdjustSpeed(0)
15 if not animationTrack.IsPlaying then
16 -- Play the animation if it is not playing
17 animationTrack:Play()
18 end
19 -- Jump to the desired TimePosition
20 animationTrack.TimePosition = (percentagePosition / 100) * animationTrack.Length
21end
22
23local animation = Instance.new("Animation")
24animation.AnimationId = "rbxassetid://507765644"
25
26local humanoid = script.Parent:WaitForChild("Humanoid")
27local animator = humanoid:WaitForChild("Animator")
28local animationTrack = animator:LoadAnimation(animation)
29
30freezeAnimationAtTime(animationTrack, 0.5)
31freezeAnimationAtPercent(animationTrack, 50)
Playing Animation for a Specific Duration

1local function playAnimationForDuration(animationTrack, duration)
2 local speed = animationTrack.Length / duration
3 animationTrack:AdjustSpeed(speed)
4 animationTrack:Play()
5end
6
7local animation = Instance.new("Animation")
8animation.AnimationId = "rbxassetid://507765000"
9
10local humanoid = script.Parent:WaitForChild("Humanoid")
11local animator = humanoid:WaitForChild("Animator")
12
13local animationTrack = animator:LoadAnimation(animation)
14
15playAnimationForDuration(animationTrack, 3)

Stop

void

Stops the AnimationTrack. Once called playback of the AnimationTrack will stop and the weight of the animation will move towards zero over a length of time specified by the optional fadeTime parameter.

For example, if Stop is called with a fadeTime of 2 seconds it will take two seconds for the weight of the AnimationTrack to reach zero and its effects completely end. Please note this will be the case regardless of the initial weight of the animation.

It is not recommended to use a fadeTime of 0 seconds to try to override this effect and end the animation immediately as presently, this causes the AnimationTrack poses to freeze.

Parameters

fadeTime: number

The time, in seconds, for which animation weight is to be faded out over.

Default Value: "0.100000001"

Returns

void

Code Samples

AnimationTrack Stop

1local function fadeOut(animationTrack, fadeTime)
2 animationTrack:Stop(fadeTime)
3 local startTime = tick()
4 while animationTrack.WeightCurrent > 0 do
5 task.wait()
6 end
7 local timeTaken = tick() - startTime
8 print("Time taken for weight to reset: " .. tostring(timeTaken))
9end
10
11local animation = Instance.new("Animation")
12animation.AnimationId = "rbxassetid://507765644"
13
14local humanoid = script.Parent:WaitForChild("Humanoid")
15local animator = humanoid:WaitForChild("Animator")
16local animationTrack = animator:LoadAnimation(animation)
17
18animationTrack:Play()
19
20fadeOut(animationTrack, 1)