Im using a script that plays an animation when a player presses a key... but whenever they hold the key down and then click... it stops the animation for a split second. Is there a way to prevent this?
Script:
local p = game.Players.LocalPlayer local anim = script.Animation local serv = game:GetService("UserInputService") local keyCodeW = Enum.KeyCode.W local char = p.Character if not p.Character then repeat wait() until p.Character end local human = char:WaitForChild("Humanoid") local newAnim = human:LoadAnimation(anim) wait(5) serv.InputBegan:Connect(function(key,processed) if serv:IsKeyDown(keyCodeW) then print("Starting animation!") newAnim:Play() end end) serv.InputEnded:Connect(function(key,processed) if not serv:IsKeyDown(keyCodeW) then newAnim:Stop() end end)
Seems like the problem is with the implementation:
InputBegin listens to any input that begins, and your function only plays the animation if the W
key gets pressed down. Good job.
InputEnded listens to any input that ends, and your function stops the animation under this condition: not serv:IsKeyDown(keyCodeW)
. I get what you're trying to say: "If the W key is not down", but that's not what this line of code means. Instead, it makes the animation stop for any input end that isn't W.
InputEnded tells you what key was released, so if key == keyCodeW
means that the W
key was released, then you could stop the animation under that condition.
Hope that helps!