Animation issue

Hi, im pretty new to roblox studio and recently i got a little but very annoying problem. I am trying to animate character when moving to the left, to the right or back if shift lock is working. So far everything works, but i got some bug which i can’t fix. When moving to two opposite sides at the same time (forward-back/right-left), the character stops. That’s ok. Then, for example, if my first input is A, then stopped by also pressing D, if I stop holding D for some reason the animation of D input is being played instead of A.
Here is the code:

UIS.InputBegan:Connect(function(Input,IsTyping)
    if UIS.MouseBehavior == Enum.MouseBehavior.LockCenter then
        local connection
        print("1")
        RunService.Stepped:Wait()

        local function runnn(s)
            if Humanoid.MoveDirection.Magnitude > 0 then
                if not IsTyping then
                    if Input.KeyCode == Enum.KeyCode.W then
                        connection:Disconnect()
                        MovingFW = true
                        MoveFW:Play()
                        MoveL:Stop()
                        MoveBW:Stop()
                        MoveR:Stop()
                    elseif Input.KeyCode == Enum.KeyCode.S then
                        connection:Disconnect()
                        MovingBW = true
                        MoveBW:Play()
                        MoveL:Stop()
                        MoveFW:Stop()
                        MoveR:Stop()
                    elseif Input.KeyCode == Enum.KeyCode.A then
                        connection:Disconnect()
                        if MovingBW == false and MovingFW == false then
                            MoveR:Play()
                            MoveL:Stop()
                        end
                        MovingR = true
                    end
                end
            else
                MoveFW:Stop()
                MoveL:Stop()
                MoveR:Stop()
                MoveBW:Stop()
            end
        end
        connection = Humanoid.Running:Connect(runnn)
    else 
        MoveFW:Stop()
        MoveBW:Stop()
        MoveR:Stop()
        MoveL:Stop()
    end
    
end)
UIS.InputEnded:Connect(function(Input,IsTyping)
    if Input.KeyCode == Enum.KeyCode.W then
        MovingFW = false
        MoveFW:Stop()
        if MovingL == true then
            MoveL:Play()
        elseif MovingR == true then
            MoveR:Play()
        end
    elseif Input.KeyCode == Enum.KeyCode.S then
        MovingBW = false
        MoveBW:Stop()
        if MovingL == true then
            MoveL:Play()
        elseif MovingR == true then
            MoveR:Play()
        end
    elseif Input.KeyCode == Enum.KeyCode.A then
        MovingL = false
        MoveL:Stop()
        if MovingR == true then
            MoveR:Play()
        end
    elseif Input.KeyCode == Enum.KeyCode.D then
        MovingR = false
        MoveR:Stop()
        if MovingL == true then
            MoveL:Play()
        end
    end
end)