Lesson 2
Play animations from code
Goal: Load and play an animation on a character.
about 6 min3 practice exercises
A published animation is played through the character's Animator, which sits inside the Humanoid.
-- LocalScript in StarterCharacterScripts
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local wave = Instance.new("Animation")
wave.AnimationId = "rbxassetid://0" -- your animation's asset ID
local track = animator:LoadAnimation(wave)
track:Play()
Three objects are involved:
- An Animation holds the asset ID.
animator:LoadAnimationturns it into an AnimationTrack for this character.- The track is what you
Play()andStop().
Load a track once and keep it. Loading it again every time the player clicks wastes work.
An animation that plays but looks wrong is usually a priority problem. A wave set to Core is hidden by the walk animation, which has a higher priority. Publish action animations as Action.
If nothing plays at all, check the asset ID and who owns the animation. Publishing it under the same user or group that owns the game is the dependable setup.
Sources
Checked against Roblox's documentation on . The lesson is in our own words; these pages are the source of truth.