Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make this Animation Local Script work?

Asked by 9 years ago

I HAVE TRIED! OK?! I HAVE TRIED!! SHEESH!! It's pretty obvious, I want an animation script to run and loop when Q is pressed and when Q is released, it obviously stops.. can you help? I mean, I got the animation working! Like, when I press Q it runs and all but I want the walkspeed to increase to 24.. but I also want it so, when you press Q again, it stops and the walkspeed returns to 16.

-- But what if I want to stop this animation? I tried to put 

wait(2)

local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()

Mouse.KeyDown:connect(function(key)
if key == "q" then
s = player.Character.Humanoid:LoadAnimation(game.StarterPack.LocalScript.Animation)
s:Play()
else
    if key == "q" then
        s = player.Character.Humanoid:LoadAnimation(game.StarterPack.Localscript.Animation)
        s:Stop()
    end
end
end)

-- But idk if this is right. Oh! And I want mu walkspeed to be 24 when I press Q and the animation activates. Can you please help?

0
Yes this was in a LocalScript in StarterPack 789zaya 0 — 9y

1 answer

Log in to vote
0
Answered by
iFlusters 355 Moderation Voter
9 years ago

Animations can be added to a place either through a script or by adding an Animation object to the Workspace.

An animation object can be added to the Workspace by selecting Insert Basic Object > Animation The only property that has to be set is the AnimationId

local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=144884906"

Playing Animation in Player Character

A model to be animated must have several parts to work. Firstly, it must have a Humanoid in which to load the animation itself with the LoadAnimation function. Secondly, it must also have a part called HumanoidRootPart. When it creates the animation from a timeline, the Animation Editor expects this part to exist. By default, player characters have both of these parts so only the animation has to be loaded in that case.

Loading the animation into a Humanoid creates an AnimationTrack which can then be played:

local animTrack = Humanoid:LoadAnimation(animation)
animTrack:Play()

Player character animations should be played from a LocalScript.

An Example:

-- Import animation
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=144911345"

-- Local variables
local animTrack = nil
local canPlay = true

function playShockAnim(Source)
    if canPlay then
        local player = game.Players.LocalPlayer.Character
        canPlay = false
        animTrack = player.Humanoid:LoadAnimation(animation) -- Load animation into Humanoid
        animTrack.KeyframeReached:connect(function(keyframeName) -- Bind function to KeyframeReached event
            if keyframeName == "ElectrocuteEnd" then
                canPlay = true
            end
        end)    
        animTrack:Play() -- Start the animation
    end
end
0
Thanks, but.. this looks like it's straight out of the Wiki.. 789zaya 0 — 9y
0
Edited from the wiki, did it help you? If so click the accept answer :) iFlusters 355 — 9y
Ad

Answer this question