I have a script that plays an animation whenever W,A,S, or D are pressed . The animation ends in a loop, however I was wondering if there was a way I could get it to stop the animation when the key was released? Would it work with a looped animation? Do I need to remove the loop? I've tried adding an additional chunk of code using Service.InputEnded but the animation continued to play.
--EDIT-- Script now fires when key is released, And I've changed the animation to one that doesn't loop. It still doesn't work quite right however.
01 | local Service = game:GetService( "UserInputService" ) |
02 | local animation = Instance.new( "Animation" ) |
03 | local player = game.Players.LocalPlayer |
04 | local char = player.Character |
05 |
06 | Service.InputBegan:connect( function (input, recieved) |
07 | if input.UserInputType = = Enum.UserInputType.Keyboard then |
08 | if input.KeyCode = = Enum.KeyCode.W then |
09 |
10 | repeat |
11 |
12 | print ( 'Input W' ) |
13 | animation.AnimationId = "http://www.roblox.com/Asset?ID=313344129" |
14 | local animTrack = player.Character.Humanoid:LoadAnimation(animation) |
15 | animTrack:Play() |
Its better to use the humanoid running function to make animations I also recommend making a function that plays the animations for you.
01 | --[[local animation = Instance.new("Animation") Our first change is that instead of doing this we do this]] |
02 | local walkAnimation = Instance.new( "Animation" ) --next we will already set the ID |
03 | walkAnimation.AnimationId = "rbxassetid://313344129" --[[I recommend using rbxassetid:// it is quicker and saves time]] |
04 | --Next we add a wait so we can have the player load |
05 | wait( 1 ) |
06 | local player = game.Players.LocalPlayer |
07 | local char = player.Character or player.Character:Added() --without this it could easily bug online |
08 | local human = char:FindFirstChild( "Humanoid" ) --allows us to get the humanoid to load animations |
09 | local playingWalkAnimation = false --this is our debounce read down for more on it |
10 | --Now we will make an function that plays animations for us! |
11 | function playAnimation(animation) |
12 | --Next we make an animation tracker right here in this function |
13 | local aniTrack = human:LoadAnimation(animation) --we will call it this aniTrack and load the animation right now |
14 | aniTrack:Stop() --we stop the animation first then wait |
15 | wait() |
Hope this helped! ~KIHeros (There may be a few errors in this please tell me in the comments!)