Using Animations in Games
Using Animations in Games
Animations truly bring a game to life. From easily-accessible character animations in the catalog to detailed animations built with the Articles/using animation editor|Animation Editor, Roblox offers a variety of powerful animation options.
Changing Default Animations
By default, Roblox player characters include common animations like running, climbing, swimming, and jumping. However, these animations are not locked in place — if desired, you can replace them with catalog animations or even load in your own Articles/using animation editor|custom animations.
Locating an Animation ID
To change a default animation, you’ll need to locate the asset ID of the new animation:
- For animations in the Avatar Animations section of the catalog, locate a specific animation (not a pack/bundle) and copy its ID from the URL in your browser.
- For animations you’ve created and exported through the
Articles/using animation editor|Animation Editor, locate the animation in the Animations section of the Create page and copy its ID from the URL in your browser.
Changing an Animation
Within every player character, a LocalScript named Animate governs which animations should be used for various character actions. To change the animation for a given action:
- Insert a
Scriptinto ServerScriptService containing the following code:
local Players = game:GetService("Players")
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
for _, playingTracks in pairs(humanoid:GetPlayingAnimationTracks()) do
playingTracks:Stop(0)
end
local animateScript = character:WaitForChild("Animate")
end
local function onPlayerAdded(player)
if player.Character then
onCharacterAdded(player.Character)
end
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
- Starting on line 12, replace the desired default animation(s) by resetting the Animate script’s
Animation/AnimationId|AnimationIdvalues to the corresponding asset ID.
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
for _, playingTracks in pairs(humanoid:GetPlayingAnimationTracks()) do
playingTracks:Stop(0)
end
local animateScript = character:WaitForChild("Animate")
animateScript.idle.Animation1.AnimationId = "rbxassetid://5432167890"
end
| Character Action | Animations |
|---|---|
| Cheering | cheer.CheerAnim |
| Climbing | climb.ClimbAnim |
| Dancing | dance.Animation1 dance.Animation2 dance.Animation3 dance2.Animation1 dance2.Animation2 dance2.Animation3 dance3.Animation1 dance3.Animation2 dance3.Animation3 |
| Falling | fall.FallAnim |
| Idle | idle.Animation1 idle.Animation2 |
| Jumping | jump.JumpAnim |
| Laughing | laugh.LaughAnim |
| Pointing | point.PointAnim |
| Running | run.RunAnim |
| Sitting | sit.SitAnim |
| Swimming | swim.Swim swimidle.SwimIdle |
| Using Tools | toollunge.ToolLungeAnim toolnone.ToolNoneAnim toolslash.ToolSlashAnim |
| Walking | walk.WalkAnim |
| Waving | wave.WaveAnim |
Using Multiple Animations
Multiple animations can be used for the same action — note, for instance, that there are two default “idle” animations. When multiple animations exist for a character state, the Animate script will randomly choose which one to play, although the outcome can be influenced by changing the animation’s Weight NumberValue (higher values increase the probability).
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
for _, playingTracks in pairs(humanoid:GetPlayingAnimationTracks()) do
playingTracks:Stop(0)
end
local animateScript = character:WaitForChild("Animate")
animateScript.idle.Animation1.AnimationId = "rbxassetid://5432167890"
-- Assign weight of 5 to 'idle.Animation1'
animateScript.idle.Animation1.Weight.Value = 5
animateScript.idle.Animation2.AnimationId = "rbxassetid://1234509876"
-- Assign weight of 10 to 'idle.Animation2'
animateScript.idle.Animation2.Weight.Value = 10
end
In the above script, this means that Animation1 will play ⅓ of the time while Animation2 will play ⅔ of the time.
Playing Animations Directly
When using custom animations built with the Articles/using animation editor|Animation Editor, you’ll often need to play an animation directly from inside a script, for instance when the player presses a certain key, picks up a special item, etc.
Player Characters
To apply an animation to a player character, it must contain a Humanoid object in order to load the animation via Humanoid/LoadAnimation|Humanoid:LoadAnimation(), as well as a part named HumanoidRootPart. Consider this example LocalScript:
local Players = game:GetService("Players")
local character = Players.LocalPlayer.Character
if not character then
character = Players.LocalPlayer.CharacterAdded:Wait()
end
local humanoid = character:WaitForChild("Humanoid")
-- Create new 'Animation' instance
local kickAnimation = Instance.new("Animation")
-- Set its 'AnimationId' to the corresponding animation asset ID
kickAnimation.AnimationId = "rbxassetid://5432167890"
-- Load animation onto the humanoid
local kickAnimationTrack = humanoid:LoadAnimation(kickAnimation)
-- Play animation track
kickAnimationTrack:Play()
-- Connect 'KeyframeReached' event to a specific named keyframe
kickAnimationTrack.KeyframeReached:Connect(function(keyframeName)
if keyframeName == "KickEnd" then
kickAnimationTrack:Play()
end
end)
This code waits for the local player’s Humanoid object to load, then it creates a new Animation instance with the proper Animation/AnimationId|AnimationId. The animation is then loaded onto the humanoid, creating an AnimationTrack, and the track is played with AnimationTrack/Play|AnimationTrack:Play(). This script also utilizes the AnimationTrack/KeyframeReached|KeyframeReached event to detect when a specific named keyframe is reached, at which point it restarts the animation.
Non-Player Characters
Using animations for NPCs is similar to using them for player characters, but it’s not essential that the NPC contains a Humanoid object. If an NPC does not contain a Humanoid, however, you must load/play the animation from an AnimationController object parented to the NPC.
Consider this simple Script which is assumed to be a direct child of the NPC model:
-- Create new 'Animation' instance
local kickAnimation = Instance.new("Animation")
-- Set its 'AnimationId' to the corresponding animation asset ID
kickAnimation.AnimationId = "rbxassetid://5432167890"
-- Create a new 'AnimationController' instance, parented to the NPC
local animController = Instance.new("AnimationController", script.Parent)
-- Load animation onto the animation controller
local kickAnimationTrack = animController:LoadAnimation(kickAnimation)
-- Play animation track
kickAnimationTrack:Play()
- NPC animations should be controlled by a
Script, not aLocalScript. - You can not control animations through both a
HumanoidandAnimationControlleron the same rig.