PcoWSkbVqDnWTu_dm2ix
Scripting Avatar Animations
Part 1 - Scripting Avatar Animations
Scripting Avatar Animations
Part 1 - Scripting Avatar Animations

Scripts can be used to update default animations and to add new ones. The two examples covered by this course 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.
    local Players = game:GetService("Players")
    local runAnimation = "rbxassetid://656118852"

    Ninja Run

    656118852

    Werewolf Run

    1083216690

    Zombie Run

    616163682

    Additional animations can be found in the Catalog Animations page.


  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.

    local Players = game:GetService("Players")
    local runAnimation = "rbxassetid://616163682"
    
    local function onCharacterAdded(character)
    
    end
    
    local function onPlayerAdded(player)
        player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
    end
    
    Players.PlayerAdded:Connect(onPlayerAdded)

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.

    local Players = game:GetService("Players")
    local runAnimation = "rbxassetid://616163682"
    
    local function onCharacterAdded(character)
        local humanoid = character:WaitForChild("Humanoid")
    end
    
    local function onPlayerAdded(player)
        player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
    end
    
    Players.PlayerAdded:Connect(onPlayerAdded)
  2. Attached to the humanoid is a script named Animate, where default animations are parented. Store this in a variable named animateScript.

    local Players = game:GetService("Players")
    local runAnimation = "rbxassetid://616163682"
    
    local function onCharacterAdded(character)
        local humanoid = character:WaitForChild("Humanoid")
        
        local animateScript = character:WaitForChild("Animate")
    end
    
    local function onPlayerAdded(player)
        player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
    end
    
    Players.PlayerAdded:Connect(onPlayerAdded)
    
  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.

    local Players = game:GetService("Players")
    local runAnimation = "rbxassetid://616163682"
    
    local function onCharacterAdded(character)
        local humanoid = character:WaitForChild("Humanoid")
        
        local animateScript = character:WaitForChild("Animate")
        animateScript.run.RunAnim.AnimationId = runAnimation
    end
    
    local function onPlayerAdded(player)
        player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
    end
    
    Players.PlayerAdded:Connect(onPlayerAdded)

    A few of the most common animations are listed below:

    animateScript.climb.ClimbAnim

    animateScript.sit.SitAnim

    animateScript.fall.FallAnim

    animateScript.swim
    animateScript.idle.Animation1 animateScript.walk.WalkAnim

    Remember to access each one using .AnimationId at the end. For a full guide on changing other default animations, see the Using Animations article.


  4. Test the game and notice how the default run animation has changed.


Next Page Playing Animations