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

How can I make a animation play when a key is pressed?

I have made an animation but I don't know how to make it play when a key is pressed this is just for one of my games. The game is non Filtering Enabled so don't worry about remotes and stuff.

2 comments

0

You need to post scripts you've tried already. Don't just expect us to script your game for you.

0

Remember to use the search bar before posting.

2 answers

You can use the UserInputService to detect a key pressed then load the animation like this, it also doesn't matter if its Filtering Enabled or not.

local p = game.Players.LocalPlayer
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=id goes here"

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
local KeyCode = input.KeyCode
if not gameProcessedEvent then

if KeyCode == Enum.KeyCode.E then -- Change E to any key you want
local animTrack = p.Character.Humanoid:LoadAnimation(animation)
animTrack:Play()

end
end
end)

12 comments

0

Error on this line "if KeyCode == Enum.KeyCode.'Key' then" '' expected near "key"

0

Also I tried putting the letter that I wanted to push to play which is 'R' it still has the same error but when I hover my mouse over it, it says "expected identifier got 'key'

0

Your suppose to put the key that you want. Like KeyCode.E or something

0

Updated the script for you

0

Sorry about that

0

There's another one now, Its "attempt to index global 'p' (a nil value)" sorry for all these questions i'm not a very good scripter I only know some basics that's why I can't do any of this.

0

Lols don't worry its fine. I had the same issues before. Make sure your using this in a LOCALSCRIPT. And needs to be in Starterpack, or Starterplayerscripts

0

upvalue 'p' sorry.

1

It worked thank you so much :)

0

Is vupvalue 'p' an error?

0

It work's fine when i run it

0

Oh, no problem :)

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