In Lines 6 and 7, you try passing off a number value (assuming its the animation ID) within the parameters of the function(s). Problem with that is the script isn't going to know what to do with those values, they are just there. There are a number of workarounds for this problem, but here's what I would do in this scenario:
Place the LocalScript
inside of StarterPack
, and insert an Animation
object directly inside of that script. It should look something like this now:
Click here to view image.
Click on the Animation
object and inside the AnimationId
property, let's enter in the asset URL in order for the script to be able to locate it: rbxassetid://319782924
View the GIF here if you have any troubles.
Sweet, now that we got the animation set up, let's head into the actual scripting. I find it better to use UserInputService
instead of KeyDown
events (especially across multiple platforms), but it works either way in this case.
03 | local key_input = game:GetService( "UserInputService" ) |
05 | local player = game.Players.LocalPlayer |
06 | local mouse = player:GetMouse() |
08 | repeat wait() until player.Character |
10 | local char = player.Character |
11 | local anim = script:WaitForChild( "Animation" ) |
15 | key_input.InputBegan:connect( function (key) |
16 | if (key.KeyCode = = Enum.KeyCode.Q) and not deb then |
20 | local animtrack = char.Humanoid:LoadAnimation(anim) |
23 | print ( "Playing animation..." ) |
We should now have a working animation script that runs whenever the Q key is pressed! Here is a preview of how the script should work (NOTE: I'm using one of my own animations for this test):
Preview GIF.
Questions / comments? Feel free to message me and I'll answer them as soon as possible. If you are satisfied with the answer I provided, please upvote me. Thanks!