How To Make Players Do Animations With A Command Roblox
The second fashion of using animations is to play them in response to a grapheme's action in-game: for case, if a player picks up an particular, 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 tin can walk up to a button and press it to activate an event.
-
Download the Shock Push button model and insert it into Studio.
Models tin can exist added into your Inventory to exist used in whatever game.
-
In a browser, open the model folio, 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 push. And 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 information technology into your script.local Players = game:GetService("Players") local actor = Players.LocalPlayer local character = actor.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.Push 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 thespian uses are stored on the role player'due south Animator object. To play the shock blitheness, a new animation track volition demand to be loaded onto the Animator object when they join the game.
-
Above
onShockTrigger
, create a new Blitheness instance namedshockAnimation
. So, set theAnimationID
of that to the desired animation. Utilize 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("Blitheness") shockAnimation.AnimationId = "rbxassetid://3716468774" local part onShockTrigger(player) end
-
Create a new variable named
shockAnimationTrack
. On the player'southward Animator, callLoadAnimation
, passing in the previously created animation.local shockAnimation = Example.new("Blitheness") shockAnimation.AnimationId = "rbxassetid://3716468774" local shockAnimationTrack = Animator:LoadAnimation(shockAnimation)
-
With the new blitheness loaded, change some of the track's properties.
- Ready the AnimationPriority to
Action
- Ensures the blitheness overrides whatever electric current animations playing. - Set Looped to
false
and 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
- Ready the AnimationPriority to
Play the Animation
Whenever someone triggers the ProximityPrompt on the button, it'll play an blitheness and temporarily freeze that actor.
-
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 blitheness plays, change the humanoid's
WalkSpeed
property to 0.local function onShockTrigger(player) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 stop
Using Animations with Events
Simply how parts have Touched events, animations have events such equally AnimationTrack.Stopped
. For the script, once the animation finishes, you'll restore the player's move speed.
-
Access the
Stopped
event for the blitheness track using the dot operator, then call theLook
function. This pauses the lawmaking until that animation finishes.local function onShockTrigger(player) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 shockAnimationTrack.Stopped:Wait() end
-
Render the player's walk speed to sixteen, the default for Roblox players.
local function onShockTrigger(histrion) 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 East to go a stupor.
The framework in this script can be easily adapted to different gameplay situations. For instance, endeavor playing a special animation whenever a player touches a trap part, or whenever a squad wins a game round.
Source: https://developer.roblox.com/en-us/onboarding/scripting-avatar-animations/2
Posted by: campbellhadeare.blogspot.com
0 Response to "How To Make Players Do Animations With A Command Roblox"
Post a Comment