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

Animation stops when a player clicks?

Asked by
exarlus 72
5 years ago

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)
0
That's because that's how your code is written. If the key is not == w then it stops the animation. TiredMelon 405 — 5y
0
Clicking is a considered a key by the way if that's what you are confused about. TiredMelon 405 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Seems like the problem is with the implementation:

  1. InputBegin listens to any input that begins, and your function only plays the animation if the W key gets pressed down. Good job.

  2. 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!

Ad

Answer this question