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

How do I make an animation "run on key" script?

Asked by 10 years ago

I want to have a crouching animation to run on a key using

if (key=="c") then

Can anyone help? I haven't been able to find anything that was useful enough. Thanks! ~pokabu3

3 answers

Log in to vote
1
Answered by
AxeOfMen 434 Moderation Voter
10 years ago

When "c" is pressed, this will play an animation named "Crouch" which is placed as a sibling to the script object

local h = player.Character:FindFirstChild("Humanoid")
local mouse = player:GetMouse()
local animation = script.Parent:WaitForChild("DrinkAnimation")
mouse.KeyUp:connect(function(key)
    if key == "c" then
        local anim = h:LoadAnimation(animation)
        anim:Play()
    end
end)

Hope this helps!

Ad
Log in to vote
1
Answered by 10 years ago

It didn't work, am I doing something wrong?

local h = player.Character:FindFirstChild("Humanoid")
local mouse = player:GetMouse()
local animation = script.Parent:WaitForChild("CrouchAnimation")
mouse.KeyUp:connect(function(key)
    if key == "c" then
        local anim = h:LoadAnimation(animation)
        anim:Play()
    end
end)

The animation is named CrouchAnimation and I inputted it into the local script itself. ~pokabu3

Log in to vote
-1
Answered by 9 years ago

Your starterpack should look like this..

-StarterPack
  -LocalScript
     -Animation 

( http://www.roblox.com/Asset?ID= PUT ID OF ANIM HERE) Go into properties and put this in the AnimID

Put this script inside of a LocalScript in StarterPack.

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()
end
end)

Answer this question