Scripting Avatar Animations

Scripts can be used to update default animations and to add new ones. The two examples covered by this tutorial will change the default run animation and will play an animation on command when a player touches an object.

Changed Default Run
Playing Animations on Command

Changing Default Animations

By default, Roblox characters include common animations like running, climbing, and jumping. For the first example, you'll create a script to swap the default run animation with a more unique one. If you don't have a run animation to practice with, you can use one of the example animations provided.

Setup the Script

So the animation swap applies to all players, the script will be stored in ServerScriptService.

  1. In ServerScriptService, create a new script named ChangeRunAnimation.

    alt

  2. In the script, create two variables:

    • Players - Gets the Players service, giving you access to players that join the game.
    • runAnimation - Sets the ID of the animation to be used. For the ID, use the one made in Creating Animations, or find one from the card below.

    1local Players = game:GetService("Players")
    2local runAnimation = "rbxassetid://656118852"
    3
  3. Copy the highlighted code below. When players join the game through PlayerAdded, the script will check if their avatar is loaded. In the next section, you'll add code to swap animations in the onCharacterAdded function.


1local Players = game:GetService("Players")
2local runAnimation = "rbxassetid://616163682"
3
4local function onCharacterAdded(character)
5
6end
7
8local function onPlayerAdded(player)
9 player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
10end
11
12Players.PlayerAdded:Connect(onPlayerAdded)
13

If you need a running animation, use one of the following example IDs. Additional catalog animations can be found on the Using Animations page.

Animation ID

Ninja Run

656118852

Werewolf Run

1083216690

Zombie Run

616163682

Replace the Animation

Default animations are accessed through a player's Humanoid object. In this case, you'll use the humanoid to find the run animation, then swap it's animation ID with a new one.

  1. In onCharacterAdded, create a variable to store the humanoid.


    1local Players = game:GetService("Players")
    2local runAnimation = "rbxassetid://616163682"
    3
    4local function onCharacterAdded(character)
    5 local humanoid = character:WaitForChild("Humanoid")
    6end
    7
    8local function onPlayerAdded(player)
    9 player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
    10end
    11
    12Players.PlayerAdded:Connect(onPlayerAdded)
    13
  2. Attached to the humanoid is a script named Animate, where default animations are parented. Store this in a variable named animateScript.


    1local Players = game:GetService("Players")
    2local runAnimation = "rbxassetid://616163682"
    3
    4local function onCharacterAdded(character)
    5 local humanoid = character:WaitForChild("Humanoid")
    6
    7 local animateScript = character:WaitForChild("Animate")
    8end
    9
    10local function onPlayerAdded(player)
    11 player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
    12end
    13
    14Players.PlayerAdded:Connect(onPlayerAdded)
    15
    16
  3. Accessing different animations can be done using the dot operator, such as animateScript.run. To change the run, set the animation ID to the one stored in runAnimation.


    1local Players = game:GetService("Players")
    2local runAnimation = "rbxassetid://616163682"
    3
    4local function onCharacterAdded(character)
    5 local humanoid = character:WaitForChild("Humanoid")
    6
    7 local animateScript = character:WaitForChild("Animate")
    8 animateScript.run.RunAnim.AnimationId = runAnimation
    9end
    10
    11local function onPlayerAdded(player)
    12 player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
    13end
    14
    15Players.PlayerAdded:Connect(onPlayerAdded)
    16
  4. Test the game and notice how the default run animation has changed.

Playing Animations

The second way of using animations is to play them in response to a character's action in-game: for instance, if a player picks up an item, or takes damage.

In this next script, whenever a player presses a button, a shock animation will play and paralyze them until the animation finishes.

Setup

The remainder of this tutorial uses a pre-made model that includes a ProximityPrompt. Players can walk up to a button and press it to activate an event.

  1. Download the Shock Button model and insert it into Studio.

    alt

  2. In StarterPlayer > StarterPlayerScripts, create a local script named PlayShockAnimation.

    alt

  3. The code below calls a function named onShockTrigger when the proximity prompt is activated. Copy it into your script.


1local Players = game:GetService("Players")
2
3local player = Players.LocalPlayer
4local character = player.Character
5if not character or not character.Parent then
6 character = player.CharacterAdded:Wait()
7end
8
9local humanoid = character:WaitForChild("Humanoid")
10local Animator = humanoid:WaitForChild("Animator")
11
12local shockButton = workspace.ShockButton.Button
13local proximityPrompt = shockButton.ProximityPrompt
14local shockParticle = shockButton.ExplosionParticle
15
16local function onShockTrigger(player)
17 shockParticle:Emit(100)
18
19end
20
21proximityPrompt.Triggered:Connect(onShockTrigger)
22

Create and Load an Animation

Animations that the player uses are stored on the player's Animator object. To play the shock animation, a new animation track will need to be loaded onto the Animator object when they join the game.

  1. Above onShockTrigger, create a new Animation instance named shockAnimation. Then, set the AnimationID of that to the desired animation. Use the ID in the code box if needed.


    1local shockButton = game.Workspace.ShockButton.Button
    2local proximityPrompt = shockButton.ProximityPrompt
    3local shockParticle = shockButton.ExplosionParticle
    4
    5local shockAnimation = Instance.new("Animation")
    6shockAnimation.AnimationId = "rbxassetid://3716468774"
    7
    8local function onShockTrigger(player)
    9
    10end
    11
  2. Create a new variable named shockAnimationTrack. On the player's Animator, call LoadAnimation, passing in the previously created animation.


    1local shockAnimation = Instance.new("Animation")
    2shockAnimation.AnimationId = "rbxassetid://3716468774"
    3
    4local shockAnimationTrack = Animator:LoadAnimation(shockAnimation)
    5
  3. With the new animation loaded, change some of the track's properties.

    • Set the AnimationPriority to Action - Ensures the animation overrides any current animations playing.
    • Set Looped to false so the animation doesn't repeat.

    1local shockAnimation = Instance.new("Animation")
    2shockAnimation.AnimationId = "rbxassetid://3716468774"
    3
    4local shockAnimationTrack = Animator:LoadAnimation(shockAnimation)
    5shockAnimationTrack.Priority = Enum.AnimationPriority.Action
    6shockAnimationTrack.Looped = false
    7

Play the Animation

Whenever someone triggers the ProximityPrompt on the button, it'll play an animation and temporarily freeze that player.

  1. Find the onShockTrigger function. On the shockAnimationTrack, call the Play function.


    1local function onShockTrigger(player)
    2 shockParticle:Emit(100)
    3
    4 shockAnimationTrack:Play()
    5end
    6
  2. To prevent the player from moving while the animation plays, change the humanoid's WalkSpeed property to 0.


    1local function onShockTrigger(player)
    2 shockParticle:Emit(100)
    3
    4 shockAnimationTrack:Play()
    5 humanoid.WalkSpeed = 0
    6end
    7

Using Animations with Events

Just how parts have Touched events, animations have events such as AnimationTrack.Stopped. For the script, once the animation finishes, you'll restore the player's move speed.

  1. Access the Stopped event for the animation track using the dot operator, then call the Wait function. This pauses the code until that animation finishes.


    1local function onShockTrigger(player)
    2 shockParticle:Emit(100)
    3
    4 shockAnimationTrack:Play()
    5 humanoid.WalkSpeed = 0
    6 shockAnimationTrack.Stopped:Wait()
    7end
    8
  2. Return the player's walk speed to 16, the default for Roblox players.


    1local function onShockTrigger(player)
    2 shockParticle:Emit(100)
    3
    4 shockAnimationTrack:Play()
    5 humanoid.WalkSpeed = 0
    6 shockAnimationTrack.Stopped:Wait()
    7 humanoid.WalkSpeed = 16
    8end
    9
  3. Test the game by walking up the part and press E to get a shock.

The framework in this script can be easily adapted to different gameplay situations. For instance, try playing a special animation whenever a player touches a trap part, or whenever a team wins a game round.