https://developer.roblox.com/en-us/onboarding/scripting-avatar-animations/2
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.
Setup
The remainder of this course uses a pre-made model that includes a ProxmityPrompt. Players can walk up to a button and press it to activate an event.
Download the Shock Button model and insert it into Studio.
How to Add Models
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
if not character or not character.Parent then
character = player.CharacterAdded:Wait()
end
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)
This script uses specifically named parts. If you rename parts of the imported model, be sure to update their variables (lines 12-14) in the script.
Create and Load an Animation
Animations that the player uses are stored on the player’s 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 named shockAnimation. Then, set the AnimationID of that to the desired animation. Use the ID in the code box if needed.
local shockButton = game.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, call LoadAnimation, 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 AnimationPriority to Action - Ensures the animation overrides any current animations playing.
Set Looped to false 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
Play the Animation
Whenever someone triggers the ProximityPrompt on the button, it’ll play an animation and temporarily freeze that player.
Find the onShockTrigger function. On the shockAnimationTrack, call the Play 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
Using Animations with Events
Just how parts have Touched events, animations have events such as 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 the Wait 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.
Previous PageScripting Avatar Animations
https://developer.roblox.com/en-us/articles/using-animations-in-games
Non-Humanoids
Playing animations on rigs that do not contain a Humanoid must be done by creating an AnimationController with a child Animator. Consider this simple Script which is assumed to be a direct child of the rig:
Animations cannot be loaded through both a Humanoid and AnimationController on the same rig. The script above handles this by looking for and destroying a Humanoid object which may exist.
local rig = script.Parent -- Destroy the humanoid object if it existslocal humanoid = rig:FindFirstChildOfClass("Humanoid")if humanoid then humanoid:Destroy()end -- Create new "Animation" instancelocal kickAnimation = Instance.new("Animation")-- Set its "AnimationId" to the corresponding animation asset IDkickAnimation.AnimationId = "rbxassetid://2515090838" -- Create a new "AnimationController" and "Animator"local animController = Instance.new("AnimationController")animController.Parent = riglocal animator = Instance.new("Animator")animator.Parent = animController -- Load animation onto the animatorlocal kickAnimationTrack = animator:LoadAnimation(kickAnimation) -- Play animation trackkickAnimationTrack:Play() -- If a named event was defined for the animation, connect it to "GetMarkerReachedSignal"kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString) print(paramString)end)