banner



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.

  1. Download the Shock Button model and insert information technology into Studio.

    alt

    Models can be added into your Inventory to exist used in whatsoever game.

    1. In a browser, open the model page, click the Become button. This adds the model into your inventory.

    2. In Studio, go to the View tab and click on the Toolbox.

    3. In the Toolbox window, click on the Inventory push. Then, brand sure the dropdown is on My Models.

      alt

    4. Select the Shock Button model to add it into the game.


  2. In StarterPlayer > StarterPlayerScripts, create a local script named PlayShockAnimation.

    alt

  3. The code below calls a function named onShockTrigger when 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.

  1. Above onShockTrigger, create a new Animation instance named shockAnimation. And so, fix the AnimationID of 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
  2. Create a new variable named shockAnimationTrack. On the histrion's Animator, call LoadAnimation, passing in the previously created blitheness.

    local shockAnimation = Example.new("Animation") shockAnimation.AnimationId = "rbxassetid://3716468774"              local shockAnimationTrack = Animator:LoadAnimation(shockAnimation)            
  3. 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 false 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            

Play the Animation

Whenever someone triggers the ProximityPrompt on the button, it'll play an animation and temporarily freeze that player.

  1. Observe the onShockTrigger office. On the shockAnimationTrack, call the Play function.

    local function onShockTrigger(player)     shockParticle:Emit(100)              shockAnimationTrack:Play()              end
  2. To prevent the actor from moving while the animation plays, change the humanoid'due south WalkSpeed property 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.

  1. Access the Stopped result for the animation rail using the dot operator, then call the Wait function. This pauses the code until that animation finishes.

    local function onShockTrigger(histrion)     shockParticle:Emit(100)      shockAnimationTrack:Play()     humanoid.WalkSpeed = 0              shockAnimationTrack.Stopped:Wait()              end
  2. 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
  3. 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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel