banner



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.

  1. Download the Stupor Push button model and insert it into Studio.

    alt

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

    1. In a browser, open up the model folio, click the Go push 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 button. And so, brand sure the dropdown is on My Models.

      alt

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


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

    alt

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

  1. In a higher place onShockTrigger, create a new Animation instance named shockAnimation. Then, set the AnimationID of 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
  2. Create a new variable named shockAnimationTrack. On the player's Animator, call LoadAnimation, passing in the previously created animation.

    local shockAnimation = Instance.new("Blitheness") shockAnimation.AnimationId = "rbxassetid://3716468774"              local shockAnimationTrack = Animator:LoadAnimation(shockAnimation)            
  3. 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 faux so 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            

Play the Animation

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

  1. Find the onShockTrigger function. On the shockAnimationTrack, phone call the Play function.

    local function onShockTrigger(histrion)     shockParticle:Emit(100)              shockAnimationTrack:Play()              end
  2. To preclude the player 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              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.

  1. Admission the Stopped event for the animation rails using the dot operator, then telephone call the Expect function. This pauses the code until that blitheness finishes.

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel