How To Make A Animation Play When You Touch Something Roblox
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 detail, or takes damage.
In this next script, whenever a player presses a push, a shock animation 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 up to a push and printing it to actuate an issue.
-
Download the Shock Button model and insert information technology into Studio.
Models can be added into your Inventory to exist used in whatsoever 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 push. Then, brand 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
onShockTriggerwhen the proximity prompt is activated. Re-create it into your script.local Players = game:GetService("Players") local player = Players.LocalPlayer local grapheme = player.Character if not character or not graphic symbol.Parent so graphic symbol = player.CharacterAdded:Await() finish 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)
Create and Load an Blitheness
Animations that the player uses are stored on the role player's Animator object. To play the shock animation, a new blitheness runway will need to be loaded onto the Animator object when they join the game.
-
Above
onShockTrigger, create a new Animation instance namedshockAnimation. And so, fix theAnimationIDof that to the desired blitheness. Apply 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 = Example.new("Animation") shockAnimation.AnimationId = "rbxassetid://3716468774" local office onShockTrigger(player) end
-
Create a new variable named
shockAnimationTrack. On the histrion's Animator, callLoadAnimation, passing in the previously created blitheness.local shockAnimation = Example.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
Activity- Ensures the animation overrides whatsoever current animations playing. - Ready Looped to
falseso 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 player.
-
Observe the
onShockTriggeroffice. On theshockAnimationTrack, call thePlayfunction.local function onShockTrigger(player) shockParticle:Emit(100) shockAnimationTrack:Play() end -
To prevent the actor from moving while the animation plays, change the humanoid'due south
WalkSpeedproperty to 0.local function onShockTrigger(player) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 terminate
Using Animations with Events
Only how parts have Touched events, animations accept events such as AnimationTrack.Stopped. For the script, in one case the blitheness finishes, you'll restore the histrion's motility speed.
-
Access the
Stoppedresult for the animation rail using the dot operator, then call theWaitfunction. This pauses the code until that animation finishes.local function onShockTrigger(histrion) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 shockAnimationTrack.Stopped:Wait() end -
Return the player'south walk speed to 16, the default for Roblox players.
local function onShockTrigger(player) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 shockAnimationTrack.Stopped:Look() humanoid.WalkSpeed = 16 end -
Examination the game past walking upwards the part and press E to get a daze.
The framework in this script can exist easily adjusted to different gameplay situations. For instance, endeavor 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: greenwoodsommestake.blogspot.com

0 Response to "How To Make A Animation Play When You Touch Something Roblox"
Post a Comment