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

How to play an Animation when you press a key?

Asked by 8 years ago

I've looked at the ROBLOX wiki, it usually just confuses me though. I've tried

if key == "q" then
runanim"Block"

I've looked at the other two, UserInputService and ContextActionService, but got more confused than i was at the start.

0
I don't want to be mean but this is so bad, you really need to watch some tutorials. I recommend you watch AlvinBlox (YouTube channel) as he is very good at explaining. PrismaticFruits 842 — 4y

2 answers

Log in to vote
1
Answered by 8 years ago

You obviously don't know how to run animations on a player, so I'll do that first.

Animating a character:

To animate a character you need an 'Animation' object. Put it in a LocalScript inside StarterPack (this will make the animation run when a player joins the game):

local anim = script.AnimationObject -- the animation I specified earlier

local p = game.Players.LocalPlayer -- the player
local char = p.Character -- their character
if not p.Character then -- checks to see if the character exists
    repeat wait() until p.Character -- if not, it waits until it exists
end
local human = char.Humanoid -- the character's humanoid (required to load an animation)

local loadAnim = human:LoadAnimation(anim) -- loads the animation into the player with the Animation object
loadAnim:Play() -- plays it

Hopefully my comments explained the script.

UserInputService (alternate version of KeyDown):

This is what you use to detect if a player has pressed a key, and it runs with events just like with KeyDown. Make the following script a LocalScript inside StartPack:

local serv = game:GetService("UserInputService")

serv.InputBegan:connect(function(key,gameProcessedEvent) -- when a player starts pressing a key, the second parameter is whether the game processed it (e.g, player is typing, so it wont process)
    print(key.keyCode)
end)

serv.InputEnded:connect(function(key) -- when a player stops pressing a key, optional for this context
    -- code
end)

To compile it all into your desired script:

The following should be put into a LocalScript inside StartPack:

local p = game.Players.LocalPlayer
local anim = script.Animation
local serv = game:GetService("UserInputService")
local char = p.Character -- their character
if not p.Character then -- checks to see if the character exists
    repeat wait() until p.Character -- if not, it waits until it exists
end
local human = char:WaitForChild("Humanoid") -- the character's humanoid (required to load an animation)

serv.InputBegan:connect(function(key,processed)
    if key.KeyCode == Enum.KeyCode.F and processed == true then -- change 'F' to your key, also checks to see if a player is typing (etc.) or not
        print("Starting animation!")
        local newAnim = human:LoadAnimation(anim)
        newAnim:Play()
    end
end)

Hope I helped :)

~TDP

0
I have a question, what does the animation have to be called? ChestplateCannon 0 — 6y
Ad
Log in to vote
0
Answered by 8 years ago

Well.

Don't use KeyDown, as it's deprecated.

Which means ROBLOX may remove it at any time.

I would recommend learning the basics of lua before going into keyDown and Inputbegan events.

Answer this question