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

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

Asked by 6 years ago

This question already has an answer here:

How to use a Animation on KeyDown?

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.

0
You need to post scripts you've tried already. Don't just expect us to script your game for you. Viking359 161 — 6y
0
Remember to use the search bar before posting. Goulstem 8144 — 6y

Marked as Duplicate by Goulstem

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?

2 answers

Log in to vote
1
Answered by
H4X0MSYT 536 Moderation Voter
6 years ago

http://wiki.roblox.com/index.php?title=API:Class/Humanoid/LoadAnimation Try this. When a key is pressed, load the animation into the humanoid and play it.

Ad
Log in to vote
1
Answered by
oSyM8V3N 429 Moderation Voter
6 years ago
Edited 6 years ago

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)
0
Error on this line "if KeyCode == Enum.KeyCode.'Key' then" '<name>' expected near "key" KeiranKiller -2 — 6y
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' KeiranKiller -2 — 6y
0
Your suppose to put the key that you want. Like KeyCode.E or something oSyM8V3N 429 — 6y
0
Updated the script for you oSyM8V3N 429 — 6y
View all comments (8 more)
0
Sorry about that oSyM8V3N 429 — 6y
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. KeiranKiller -2 — 6y
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 oSyM8V3N 429 — 6y
0
upvalue 'p' sorry. KeiranKiller -2 — 6y
1
It worked thank you so much :) KeiranKiller -2 — 6y
0
Is vupvalue 'p' an error? oSyM8V3N 429 — 6y
0
It work's fine when i run it oSyM8V3N 429 — 6y
0
Oh, no problem :) oSyM8V3N 429 — 6y