PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Using Animations in Games

Using Animations in Games

Oct 11 2019, 1:08 PM PST 10 min

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.

Using Catalog Animations

To use an animation in the catalog, you’ll first need to locate its asset ID as follows:

  1. Navigate to the Avatar Animations section. Although most animation packs cost a certain amount of Robux () for players, you do not need to purchase a pack to use the animations it contains.
  2. Choose an animation pack such as “Ninja Animation Package” and click it.
  3. Scroll down to the Included Items section and click on an animation you’d like to use.
  4. Copy its ID from the URL in your browser.

Using Custom Animations

To use an animation you’ve exported through the Articles/using animation editor|Animation Editor, locate its asset ID as follows:

  1. Open the Animations section of the Create page.
  2. Locate the desired animation and click it.
  3. Copy its ID from the URL in your browser.

Changing a Default Animation

Once you have a valid animation asset ID, you can easily replace any of the default Roblox character animations:

  1. Insert a Script into 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)
	player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)
  1. Starting on line 11, replace the desired default animation(s) by resetting the Animate script’s Animation/AnimationId|AnimationId values to the corresponding asset ID:
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")
	animateScript.climb.ClimbAnim.AnimationId = "rbxassetid://658833139"
end

local function onPlayerAdded(player)
	player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

The following reference chart lists the default animations which can be replaced, for instance animateScript.idle.Animation1.AnimationId.

Character Action Animation References
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 Animation Weight

Multiple animations may 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 value.

When assigning weight values, the probability of an animation being chosen is:

In the script below, this means that Animation1 will play ⅓ of the time while Animation2 will play ⅔ of the time.

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"
	animateScript.idle.Animation2.AnimationId = "rbxassetid://1234509876"
	-- Assign weight of 5 to "idle.Animation1"
	animateScript.idle.Animation1.Weight.Value = 5
	-- Assign weight of 10 to "idle.Animation2"
	animateScript.idle.Animation2.Weight.Value = 10
end

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()
Tags:
  • animation
  • animate