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

Using Animations in Games

Using Animations in Games

Nov 09 2018, 1:59 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.

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:

  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)
	if player.Character then
		onCharacterAdded(player.Character)
	end
	player.CharacterAdded:Connect(onCharacterAdded)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)
  1. Starting on line 12, replace the desired default animation(s) by resetting the Animate script’s Animation/AnimationId|AnimationId values 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

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
Important Notes for Custom Animations

When using custom animations built via the Articles/using animation editor|Animation Editor for default character animations, note these important points:

  • Name the final keyframe in the custom animation End (with a capital E). This triggers the animation script to pick the next animation to play.
  • The animation script built into Roblox characters handles animation looping automatically, so you should disable the “Loop” option in the animation editor before publishing the animation. If you leave looping on, multiple animations will not work for the given character state.

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