Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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!

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

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.