title | description | prev |
---|---|---|
Scripting Avatar Animations |
The process for updating default animations. |
/tutorials/building/animation/creating-an-animation |
Scripts can be used to update default animations and to add new ones. The two examples covered by this tutorial will change the default run animation and will play an animation on command when a player touches an object.
Changed Default Run Playing Animations on CommandBy default, Roblox characters include common animations like running, climbing, and jumping. For the first example, you'll create a script to swap the default run animation with a more unique one. If you don't have a run animation to practice with, you can use one of the example animations provided.
So the animation swap applies to all players, the script will be stored in ServerScriptService.
-
In ServerScriptService, create a new script named ChangeRunAnimation.
-
In the script, create two variables:
Class.Players
- Gets the Players service, giving you access to players that join the game.runAnimation
- Sets the ID of the animation to be used. For the ID, use the one made in Creating Animations, or find one from the card below.
local Players = game:GetService("Players") local runAnimation = "rbxassetid://656118852"
-
Copy the highlighted code below. When players join the game through
PlayerAdded
, the script will check if their avatar is loaded. In the next section, you'll add code to swap animations in theonCharacterAdded
function.
local Players = game:GetService("Players")
local runAnimation = "rbxassetid://616163682"
local function onCharacterAdded(character)
end
local function onPlayerAdded(player)
player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
If you need a running animation, use one of the following example IDs. Additional catalog animations can be found on the Using Animations page.
Animation | ID |
---|---|
Ninja Run | 656118852 |
Werewolf Run | 1083216690 |
Zombie Run | 616163682 |
Default animations are accessed through a player's Humanoid object. In this case, you'll use the humanoid to find the run animation, then swap it's animation ID with a new one.
-
In
onCharacterAdded
, create a variable to store the humanoid.local Players = game:GetService("Players") local runAnimation = "rbxassetid://616163682" local function onCharacterAdded(character) local humanoid = character:WaitForChild("Humanoid") end local function onPlayerAdded(player) player.CharacterAppearanceLoaded:Connect(onCharacterAdded) end Players.PlayerAdded:Connect(onPlayerAdded)
-
Attached to the humanoid is a script named Animate, where default animations are parented. Store this in a variable named animateScript.
local Players = game:GetService("Players") local runAnimation = "rbxassetid://616163682" local function onCharacterAdded(character) local humanoid = character:WaitForChild("Humanoid") local animateScript = character:WaitForChild("Animate") end local function onPlayerAdded(player) player.CharacterAppearanceLoaded:Connect(onCharacterAdded) end Players.PlayerAdded:Connect(onPlayerAdded)
-
Accessing different animations can be done using the dot operator, such as
animateScript.run
. To change the run, set the animation ID to the one stored inrunAnimation
.local Players = game:GetService("Players") local runAnimation = "rbxassetid://616163682" local function onCharacterAdded(character) local humanoid = character:WaitForChild("Humanoid") local animateScript = character:WaitForChild("Animate") animateScript.run.RunAnim.AnimationId = runAnimation end local function onPlayerAdded(player) player.CharacterAppearanceLoaded:Connect(onCharacterAdded) end Players.PlayerAdded:Connect(onPlayerAdded)
A few of the other common animations you can change are listed below:
animateScript.climb.ClimbAnim
animateScript.sit.SitAnim
animateScript.fall.FallAnim
animateScript.swim
animateScript.idle.Animation1
animateScript.walk.WalkAnim
Remember to access each one using
.AnimationId
at the end. For a full guide on changing other default animations, see the Using Animations article. -
Test the game and notice how the default run animation has changed.
The second way of using animations is to play them in response to a character's action in-game: for instance, if a player picks up an item, or takes damage.
In this next script, whenever a player presses a button, a shock animation will play and paralyze them until the animation finishes.
The remainder of this tutorial uses a pre-made model that includes a ProximityPrompt. Players can walk up to a button and press it to activate an event.
-
Download the Shock Button model and insert it into Studio.
Models can be added into your Inventory to be used in any game.
- In a browser, open the model page, click the Get button. This adds the model into your inventory.
- In Studio, go to the View tab and click on the Toolbox.
- In the Toolbox window, click on the Inventory button. Then, make sure the dropdown is on My Models.
- Select the Shock Button model to add it into the game.
-
In StarterPlayer > StarterPlayerScripts, create a local script named PlayShockAnimation.
-
The code below calls a function named
onShockTrigger
when the proximity prompt is activated. Copy it into your script.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")
local shockButton = workspace.ShockButton.Button
local proximityPrompt = shockButton.ProximityPrompt
local shockParticle = shockButton.ExplosionParticle
local function onShockTrigger(player)
shockParticle:Emit(100)
end
proximityPrompt.Triggered:Connect(onShockTrigger)
Animations that the player uses are stored on the player's Class.Animator
object. To play the shock animation, a new animation track will need to be loaded onto the Animator object when they join the game.
-
Above
onShockTrigger
, create a new Animation instance namedshockAnimation
. Then, set theAnimationID
of that to the desired animation. Use the ID in the code box if needed.local shockButton = workspace.ShockButton.Button local proximityPrompt = shockButton.ProximityPrompt local shockParticle = shockButton.ExplosionParticle local shockAnimation = Instance.new("Animation") shockAnimation.AnimationId = "rbxassetid://3716468774" local function onShockTrigger(player) end
-
Create a new variable named
shockAnimationTrack
. On the player's Animator, callLoadAnimation
, passing in the previously created animation.local shockAnimation = Instance.new("Animation") shockAnimation.AnimationId = "rbxassetid://3716468774" local shockAnimationTrack = Animator:LoadAnimation(shockAnimation)
-
With the new animation loaded, change some of the track's properties.
- Set the
Enum.AnimationPriority
toAction
- Ensures the animation overrides any current animations playing. - Set
Class.AnimationTrack.Looped|Looped
tofalse
so the animation doesn't repeat.
local shockAnimation = Instance.new("Animation") shockAnimation.AnimationId = "rbxassetid://3716468774" local shockAnimationTrack = Animator:LoadAnimation(shockAnimation) shockAnimationTrack.Priority = Enum.AnimationPriority.Action shockAnimationTrack.Looped = false
- Set the
Whenever someone triggers the ProximityPrompt on the button, it'll play an animation and temporarily freeze that player.
-
Find the
onShockTrigger
function. On theshockAnimationTrack
, call thePlay
function.local function onShockTrigger(player) shockParticle:Emit(100) shockAnimationTrack:Play() end
-
To prevent the player from moving while the animation plays, change the humanoid's
WalkSpeed
property to 0.local function onShockTrigger(player) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 end
Just how parts have Touched events, animations have events such as Class.AnimationTrack.Stopped
. For the script, once the animation finishes, you'll restore the player's move speed.
-
Access the
Stopped
event for the animation track using the dot operator, then call theWait
function. This pauses the code until that animation finishes.local function onShockTrigger(player) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 shockAnimationTrack.Stopped:Wait() end
-
Return the player's walk speed to 16, the default for Roblox players.
local function onShockTrigger(player) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 shockAnimationTrack.Stopped:Wait() humanoid.WalkSpeed = 16 end
-
Test the game by walking up the part and press E to get a shock.
The framework in this script can be easily adapted to different gameplay situations. For instance, try playing a special animation whenever a player touches a trap part, or whenever a team wins a game round.