How To Make A Animation Play In Roblox
The 2d way of using animations is to play them in response to a grapheme's action in-game: for instance, if a player picks upwardly an item, or takes impairment.
In this side by side script, whenever a histrion presses a button, a shock animation volition play and paralyze them until the animation finishes.
Setup
The remainder of this course uses a pre-made model that includes a ProxmityPrompt. Players tin walk upwardly to a push and printing information technology to actuate an consequence.
-
Download the Shock Button model and insert it into Studio.
Models tin exist added into your Inventory to exist used in any game.
-
In a browser, open the model page, click the Become 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, make certain the dropdown is on My Models.
-
Select the Shock Button model to add information technology into the game.
-
-
In StarterPlayer > StarterPlayerScripts, create a local script named PlayShockAnimation.
-
The lawmaking below calls a function named
onShockTrigger
when the proximity prompt is activated. Re-create it into your script.local Players = game:GetService("Players") local role player = Players.LocalPlayer local character = thespian.Character if not character or not grapheme.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(actor) shockParticle:Emit(100) end proximityPrompt.Triggered:Connect(onShockTrigger)
Create and Load an Animation
Animations that the role player uses are stored on the histrion's Animator object. To play the shock animation, a new animation rail will demand to be loaded onto the Animator object when they join the game.
-
In a higher place
onShockTrigger
, create a new Blitheness instance namedshockAnimation
. Then, gear up theAnimationID
of that to the desired blitheness. Use 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 part onShockTrigger(player) end
-
Create a new variable named
shockAnimationTrack
. On the role player'southward Animator, telephone callLoadAnimation
, passing in the previously created blitheness.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 blitheness overrides any current animations playing. - Set Looped to
imitation
so the blitheness 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 AnimationPriority to
Play the Animation
Whenever someone triggers the ProximityPrompt on the button, it'll play an animation and temporarily freeze that actor.
-
Find the
onShockTrigger
function. On theshockAnimationTrack
, call thePlay
function.local part onShockTrigger(player) shockParticle:Emit(100) shockAnimationTrack:Play() finish
-
To prevent the actor 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, in one case the animation finishes, you'll restore the player'south move speed.
-
Access the
Stopped
event for the animation track using the dot operator, then phone call theAwait
function. This pauses the code until that animation finishes.local part onShockTrigger(actor) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 shockAnimationTrack.Stopped:Wait() end
-
Render the actor'due south 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 finish
-
Test the game by walking up the part and printing East to get a shock.
The framework in this script tin can exist easily adjusted to different gameplay situations. For case, attempt playing a special animation whenever a role player touches a trap part, or whenever a team wins a game circular.
Source: https://developer.roblox.com/en-us/onboarding/scripting-avatar-animations/2
Posted by: alvarezralmy1981.blogspot.com
0 Response to "How To Make A Animation Play In Roblox"
Post a Comment