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.
How can I make a animation play when a key is pressed?
2 answers
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.
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
Error on this line "if KeyCode == Enum.KeyCode.'Key' then" '' expected near "key"
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'
Your suppose to put the key that you want. Like KeyCode.E or something
Updated the script for you
Sorry about that
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.
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
upvalue 'p' sorry.
It worked thank you so much :)
Is vupvalue 'p' an error?
It work's fine when i run it
Oh, no problem :)
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
You need to post scripts you've tried already. Don't just expect us to script your game for you.
Remember to use the search bar before posting.