--- title: Use animations description: Explains the process of playing animations through scripts, and replacing default animations. --- Once you have [created an animation](../animation/editor.md), you need to use scripts to include them in your experience. You can either [play animations manually](#play-animations-from-scripts) from scripts or [replace default animations](#replace-default-animations) for player characters. ## Play animations from scripts In some cases, you'll need to play an animation directly from inside a script, such as when a user presses a certain key or picks up a special item. ### Humanoids To play an animation on a rig containing a `Class.Humanoid` object, such as typical playable characters, follow this basic pattern: 1. Ensure that the local player's `Class.Humanoid` contains an `Class.Animator` object. 2. Create a new `Class.Animation` instance with the proper `Class.Animation.AnimationId|AnimationId`. 3. Load the animation via `Class.Animator:LoadAnimation()` to create an `Class.AnimationTrack`. 4. Play the track with `Class.AnimationTrack:Play()`. For example, the following `Class.LocalScript`, when placed in `Class.StarterPlayerScripts`, loads a "kick" animation onto the player's character and plays it. The script also utilizes the `Class.AnimationTrack:GetMarkerReachedSignal()|GetMarkerReachedSignal()` method to detect when a specific [animation event](../animation/events.md) occurs. ```lua title="LocalScript - Play Custom Animation on Player Character" local Players = game:GetService("Players") local player = Players.LocalPlayer local character = player.Character or player.CharacterAdded:Wait() -- Ensure that the character's humanoid contains an "Animator" object local humanoid = character:WaitForChild("Humanoid") local animator = humanoid:WaitForChild("Animator") -- Create a new "Animation" instance and assign an animation asset ID local kickAnimation = Instance.new("Animation") kickAnimation.AnimationId = "rbxassetid://2515090838" -- Load the animation onto the animator local kickAnimationTrack = animator:LoadAnimation(kickAnimation) -- If a named event was defined for the animation, connect it to "GetMarkerReachedSignal()" kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString) print(paramString) end) task.wait(4) -- Play the animation track kickAnimationTrack:Play() ``` ### Non-humanoids To play animations on rigs that do **not** contain a `Class.Humanoid`, you must create an `Class.AnimationController` with a child `Class.Animator`. For example, the following `Class.Script` (assumed to be a direct child of the rig) loads a "kick" animation and plays it. ```lua title="Script - Play Custom Animation on Character Rig" local rig = script.Parent -- Create a new "Animation" instance and assign an animation asset ID local kickAnimation = Instance.new("Animation") kickAnimation.AnimationId = "rbxassetid://2515090838" -- Create a new "AnimationController" and "Animator" local animationController = Instance.new("AnimationController") animationController.Parent = rig local animator = Instance.new("Animator") animator.Parent = animationController -- Load the animation onto the animator local kickAnimationTrack = animator:LoadAnimation(kickAnimation) -- If a named event was defined for the animation, connect it to "GetMarkerReachedSignal()" kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString) print(paramString) end) task.wait(4) -- Play the animation track kickAnimationTrack:Play() ``` ## Replace default animations By default, Roblox player characters include common animations like running, climbing, swimming, and jumping. You can replace these [default animations](#default-character-animations) with animations from the [catalog](#catalog-animations) or with your own [custom](../animation/editor.md) animations. 1. Obtain the **asset ID** of the new animation as follows: - For a custom animation built with the [Animation Editor](../animation/editor.md), follow the [export](../animation/editor.md#exporting-an-animation) instructions. - Copy an appropriate ID from the [catalog animation reference](#catalog-animations) below. For example, to replace the default run animation with the Ninja Run variant, use `656118852`. 2. In the [Explorer](../studio/explorer.md) window, add a new `Class.Script` to **ServerScriptService**. 1. Hover over **ServerScriptService** and click the ⊕ button. 2. From the contextual menu, insert a **Script**. 3. In the new script, paste the following code: ```lua title="Script - Replace Default Character Animations" local Players = game:GetService("Players") local function onCharacterAdded(character) -- Get animator on humanoid local humanoid = character:WaitForChild("Humanoid") local animator = humanoid:WaitForChild("Animator") -- Stop all animation tracks for _, playingTrack in animator:GetPlayingAnimationTracks() do playingTrack:Stop(0) end local animateScript = character:WaitForChild("Animate") --animateScript.run.RunAnim.AnimationId = "rbxassetid://" --animateScript.walk.WalkAnim.AnimationId = "rbxassetid://" --animateScript.jump.JumpAnim.AnimationId = "rbxassetid://" --animateScript.idle.Animation1.AnimationId = "rbxassetid://" --animateScript.idle.Animation2.AnimationId = "rbxassetid://" --animateScript.fall.FallAnim.AnimationId = "rbxassetid://" --animateScript.swim.Swim.AnimationId = "rbxassetid://" --animateScript.swimidle.SwimIdle.AnimationId = "rbxassetid://" --animateScript.climb.ClimbAnim.AnimationId = "rbxassetid://" end local function onPlayerAdded(player) player.CharacterAppearanceLoaded:Connect(onCharacterAdded) end Players.PlayerAdded:Connect(onPlayerAdded) ``` 4. For each line that references a [default character animation](#default-character-animations), uncomment it and paste the replacement ID after `rbxassetid://`. For example, to change the default run animation to the Ninja Run variant: ```lua title="Script - Replace Default Character Animations" highlight="14" local Players = game:GetService("Players") local function onCharacterAdded(character) -- Get animator on humanoid local humanoid = character:WaitForChild("Humanoid") local animator = humanoid:WaitForChild("Animator") -- Stop all animation tracks for _, playingTrack in animator:GetPlayingAnimationTracks() do playingTrack:Stop(0) end local animateScript = character:WaitForChild("Animate") animateScript.run.RunAnim.AnimationId = "rbxassetid://656118852" --animateScript.walk.WalkAnim.AnimationId = "rbxassetid://" --animateScript.jump.JumpAnim.AnimationId = "rbxassetid://" --animateScript.idle.Animation1.AnimationId = "rbxassetid://" --animateScript.idle.Animation2.AnimationId = "rbxassetid://" --animateScript.fall.FallAnim.AnimationId = "rbxassetid://" --animateScript.swim.Swim.AnimationId = "rbxassetid://" --animateScript.swimidle.SwimIdle.AnimationId = "rbxassetid://" --animateScript.climb.ClimbAnim.AnimationId = "rbxassetid://" end local function onPlayerAdded(player) player.CharacterAppearanceLoaded:Connect(onCharacterAdded) end Players.PlayerAdded:Connect(onPlayerAdded) ``` ## Set animation weights You can use multiple animations for the same action. For example, there are two `idle` variations in the code sample for [replacing default animations](#replace-default-animations). When multiple animations exist for a character state, the **Animate** script randomly chooses which one to play, but you can influence the outcome by setting the animation's `Weight` value under the following formula: - **animation weight / total weight of all state animations** In the following example, `idle.Animation1` will play ⅓ of the time the character is idle, while `idle.Animation2` will play ⅔ of the time. ```lua title="Script - Replace Default Character Animations" highlight="3,4" animateScript.idle.Animation1.AnimationId = "rbxassetid://656117400" animateScript.idle.Animation2.AnimationId = "rbxassetid://656118341" animateScript.idle.Animation1.Weight.Value = 5 animateScript.idle.Animation2.Weight.Value = 10 ``` ## Animation references ### Default character animations The following table contains all of the default character animations that you can [replace](#replace-default-animations) with [catalog](#catalog-animations) animations or your own [custom](../animation/editor.md) animations. Note that **Idle** has two variations which you can [weight](#set-animation-weights) to play more or less frequently.
| Character action | Animate script reference |
|---|---|
| **Run** | `animateScript.run.RunAnim.AnimationId` |
| **Walk** | `animateScript.walk.WalkAnim.AnimationId` |
| **Jump** | `animateScript.jump.JumpAnim.AnimationId` |
| **Idle** |
`animateScript.idle.Animation1.AnimationId` `animateScript.idle.Animation2.AnimationId` |
| **Fall** | `animateScript.fall.FallAnim.AnimationId` |
| **Swim** | `animateScript.swim.Swim.AnimationId` |
| **Swim (Idle)** | `animateScript.swimidle.SwimIdle.AnimationId` |
| **Climb** | `animateScript.climb.ClimbAnim.AnimationId` |
| **Astronaut** |
891636393 891636393 891627522 891621366, 891633237, 1047759695 891617961 891639666 891663592 891609353 |
| **Bubbly** |
910025107 910034870 910016857 910004836, 910009958, 1018536639 910001910 910028158 910030921 909997997 |
| **Cartoony** |
742638842 742640026 742637942 742637544, 742638445, 885477856 742637151 742639220 742639812 742636889 |
| **Elder** |
845386501 845403856 845398858 845397899, 845400520, 901160519 845396048 845401742 845403127 845392038 |
| **Knight** |
657564596 657552124 658409194 657595757, 657568135, 885499184 657600338 657560551 657557095 658360781 |
| **Levitation** |
616010382 616013216 616008936 616006778, 616008087, 886862142 616005863 616011509 616012453 616003713 |
| **Mage** |
707861613 707897309 707853694 707742142, 707855907, 885508740 707829716 707876443 707894699 707826056 |
| **Ninja** |
656118852 656121766 656117878 656117400, 656118341, 886742569 656115606 656119721 656121397 656114359 |
| **Pirate** |
750783738 750785693 750782230 750781874, 750782770, 885515365 750780242 750784579 750785176 750779899 |
| **Robot** |
616091570 616095330 616090535 616088211, 616089559, 885531463 616087089 616092998 616094091 616086039 |
| **Rthro** |
2510198475 2510202577 2510197830 2510197257, 2510196951, 3711062489 2510195892 2510199791 2510201162 2510192778 |
| **Stylish** |
616140816 616146177 616139451 616136790, 616138447, 886888594 616134815 616143378 616144772 616133594 |
| **Superhero** |
616117076 616122287 616115533 616111295, 616113536, 885535855 616108001 616119360 616120861 616104706 |
| **Toy** |
782842708 782843345 782847020 782841498, 782845736, 980952228 782846423 782844582 782845186 782843869 |
| **Vampire** |
1083462077 1083473930 1083455352 1083445855, 1083450166, 1088037547 1083443587 1083464683 1083467779 1083439238 |
| **Werewolf** |
1083216690 1083178339 1083218792 1083195517, 1083214717, 1099492820 1083189019 1083222527 1083225406 1083182000 |
| **Zombie** |
616163682 616168032 616161997 616158929, 616160636, 885545458 616157476 616165109 616166655 616156119 |