How To Play An Animation On Roblox
The second mode of using animations is to play them in response to a character's activity in-game: for instance, if a player picks up an item, or takes damage.
In this next script, whenever a histrion presses a push, a stupor blitheness volition play and paralyze them until the animation finishes.
Setup
The remainder of this course uses a pre-fabricated model that includes a ProxmityPrompt. Players can walk upwardly to a push and press it to activate an event.
-
Download the Stupor Push button model and insert it into Studio.
Models tin be added into your Inventory to be used in whatsoever game.
-
In a browser, open up the model folio, click the Go push 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. And so, brand sure the dropdown is on My Models.
-
Select the Shock Button model to add together it into the game.
-
-
In StarterPlayer > StarterPlayerScripts, create a local script named PlayShockAnimation.
-
The code beneath calls a function named
onShockTriggerwhen the proximity prompt is activated. Copy information technology into your script.local Players = game:GetService("Players") local player = Players.LocalPlayer local graphic symbol = player.Grapheme if not graphic symbol or not character.Parent and so graphic symbol = player.CharacterAdded:Wait() end local humanoid = grapheme:WaitForChild("Humanoid") local Animator = humanoid:WaitForChild("Animator") local shockButton = workspace.ShockButton.Button local proximityPrompt = shockButton.ProximityPrompt local shockParticle = shockButton.ExplosionParticle local role onShockTrigger(actor) shockParticle:Emit(100) end proximityPrompt.Triggered:Connect(onShockTrigger)
Create and Load an Blitheness
Animations that the actor uses are stored on the player'southward Animator object. To play the shock animation, a new blitheness rail will demand to exist loaded onto the Animator object when they join the game.
-
In a higher place
onShockTrigger, create a new Animation instance namedshockAnimation. Then, set theAnimationIDof that to the desired blitheness. Employ the ID in the code box if needed.local shockButton = game.Workspace.ShockButton.Push 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("Blitheness") shockAnimation.AnimationId = "rbxassetid://3716468774" local shockAnimationTrack = Animator:LoadAnimation(shockAnimation) -
With the new blitheness loaded, change some of the track's backdrop.
- Prepare the AnimationPriority to
Action- Ensures the animation overrides whatsoever electric current animations playing. - Ready Looped to
fauxso the blitheness doesn't echo.
local shockAnimation = Instance.new("Animation") shockAnimation.AnimationId = "rbxassetid://3716468774" local shockAnimationTrack = Animator:LoadAnimation(shockAnimation) shockAnimationTrack.Priority = Enum.AnimationPriority.Action shockAnimationTrack.Looped = fake - Prepare the AnimationPriority to
Play the Animation
Whenever someone triggers the ProximityPrompt on the button, it'll play an animation and temporarily freeze that player.
-
Find the
onShockTriggerfunction. On theshockAnimationTrack, phone call thePlayfunction.local function onShockTrigger(histrion) shockParticle:Emit(100) shockAnimationTrack:Play() end -
To preclude the player from moving while the animation plays, change the humanoid's
WalkSpeedproperty to 0.local function onShockTrigger(player) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 finish
Using Animations with Events
Merely how parts have Touched events, animations accept events such equally AnimationTrack.Stopped. For the script, in one case the animation finishes, you'll restore the player's move speed.
-
Admission the
Stoppedevent for the animation rails using the dot operator, then telephone call theExpectfunction. This pauses the code until that blitheness finishes.local function onShockTrigger(thespian) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 shockAnimationTrack.Stopped:Wait() cease -
Return the player's walk speed to xvi, the default for Roblox players.
local function onShockTrigger(role player) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 shockAnimationTrack.Stopped:Wait() humanoid.WalkSpeed = 16 finish -
Test the game by walking up the part and press East to get a shock.
The framework in this script can be easily adjusted 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.
Source: https://developer.roblox.com/en-us/onboarding/scripting-avatar-animations/2
Posted by: kellygeression1998.blogspot.com

0 Response to "How To Play An Animation On Roblox"
Post a Comment