Once you have created an animation, you need to use scripts to include them in your experience. You can either play animations manually from scripts or replace default animations for player characters.
Playing 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 Humanoidobject, such as typical playable characters, follow this basic pattern:
Ensure that the local player's Humanoid contains an Animator object.
Create a new Animation instance with the proper AnimationId.
Load the animation via Animator:LoadAnimation() to create an AnimationTrack.
Play the track with AnimationTrack:Play().
For example, the following LocalScript, when placed inStarterPlayerScripts, loads a "kick" animation onto the player's character and plays it. The script also utilizes the GetMarkerReachedSignal() method to detect when a specific animation event occurs.
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)
-- Play the animation track
kickAnimationTrack:Play()
-- If a named event was defined for the animation, connect it to "GetMarkerReachedSignal()"
kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString)
print(paramString)
end)
Non-Humanoids
To play animations on rigs that do not contain a Humanoid, you must create an AnimationController with a child Animator. For example, the following Script (assumed to be a direct child of the rig) loads a "kick" animation and plays it.
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)
-- Play the animation track
kickAnimationTrack:Play()
-- If a named event was defined for the animation, connect it to "GetMarkerReachedSignal()"
kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString)
print(paramString)
end)
Replacing Default Animations
By default, Roblox player characters include common animations like running,climbing, swimming, and jumping. You can replace these default animations with animations from the catalog or with your own custom animations.
Obtain the asset ID of the new animation as follows:
For a custom animation built with the Animation Editor, follow the export instructions.
Copy an appropriate ID from the catalog animation reference below. For example, to replace the default run animation with the NinjaRun variant, use 656118852.
In the Explorer window, add a new Script to ServerScriptService.
Hover over ServerScriptService and click the ⊕ button.
From the contextual menu, insert a Script.
In the new script, paste the following code:
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)
For each line that references a default character animation, uncomment it and paste the replacement ID after rbxassetid://. For example, to change the default run animation to the NinjaRun variant:
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://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)
Setting Animation Weights
You can use multiple animations for the same action. For example, thereare two idle variations in the code sample for replacing default animations.
When multiple animations exist for a character state, the Animate scriptrandomly chooses which one to play, but you can influence the outcome bysetting 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.
Script - Replace Default Character Animations
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 with catalog animations or your own custom animations. Note that Idle has two variations which you can weight 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 |
Fall | animateScript.fall.FallAnim.AnimationId |
Swim | animateScript.swim.Swim.AnimationId |
Swim (Idle) | animateScript.swimidle.SwimIdle.AnimationId |
Climb | animateScript.climb.ClimbAnim.AnimationId |
Catalog Animations
When using avatar animation bundles to replace default animations, use the following references for the respective asset IDs. For example, if you want to apply the NinjaJump animation, use 656117878. Note that Idle has multiple variations.
Astronaut | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Bubbly | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Cartoony | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Elder | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Knight | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Levitation | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Mage | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Ninja | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Pirate | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Robot | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Rthro | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Stylish | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Superhero | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Toy | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Vampire | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Werewolf | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |
Zombie | Run Walk Jump Idle Fall Swim Swim (Idle) Climb |